Skip to content

Commit 31951f6

Browse files
committed
Use new Azure package
1 parent 417f702 commit 31951f6

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

  • samples/CZ.AspNetCore.EasyAuthAuthentication.Samples.Client

samples/CZ.AspNetCore.EasyAuthAuthentication.Samples.Client/Program.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace CZ.AspNetCore.EasyAuthAuthentication.Samples.Webs.Client
55
using System.Net;
66
using System.Text;
77
using System.Threading.Tasks;
8-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
8+
using Microsoft.Identity.Client;
99

1010
public class Program
1111
{
@@ -21,13 +21,16 @@ private static void Main(string[] args)
2121

2222
private static async Task Work()
2323
{
24-
var authContext = new AuthenticationContext(System.Environment.GetEnvironmentVariable("authority"));
25-
var credentials = new ClientCredential(System.Environment.GetEnvironmentVariable("clientId"), System.Environment.GetEnvironmentVariable("clientSecret"));
26-
var token = await authContext.AcquireTokenAsync(System.Environment.GetEnvironmentVariable("resource"), credentials);
27-
Console.WriteLine(token.AccessTokenType + " " + token.AccessToken);
24+
var authContext = ConfidentialClientApplicationBuilder
25+
.Create(Environment.GetEnvironmentVariable("clientId"))
26+
.WithAuthority(Environment.GetEnvironmentVariable("authority"))
27+
.WithClientSecret(Environment.GetEnvironmentVariable("clientSecret"))
28+
.Build();
29+
var token = await authContext.AcquireTokenForClient(new string[] { $"{Environment.GetEnvironmentVariable("resource")}/.default" }).ExecuteAsync();
30+
Console.WriteLine("Bearer " + token.AccessToken);
2831
var headerName = "Authorization";
2932
var httpRequest = WebRequest.Create("https://sampleappauth.azurewebsites.net/api/SampleData/UserName");
30-
httpRequest.Headers.Add(headerName, token.AccessTokenType + " " + token.AccessToken);
33+
httpRequest.Headers.Add(headerName, "Bearer " + token.AccessToken);
3134
var response = httpRequest.GetResponse();
3235
var stream = response.GetResponseStream();
3336
var reader = new StreamReader(stream);

0 commit comments

Comments
 (0)