Skip to content

Commit 2be5ffd

Browse files
authored
Merge pull request #77 from codez-one/tmfr/Fix_HandleAuthenticateAsync
Fix HandleAuthenticateAsync & Update packages
2 parents 50d9cde + 31951f6 commit 2be5ffd

7 files changed

Lines changed: 48 additions & 38 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.9" />
9+
<PackageReference Include="Microsoft.Identity.Client" Version="4.55.0" />
1010
</ItemGroup>
1111

1212
</Project>

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);

samples/CZ.AspNetCore.EasyAuthAuthentication.Samples.Web/CZ.AspNetCore.EasyAuthAuthentication.Samples.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.30" />
17+
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.32" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

src/CZ.AspNetCore.EasyAuthAuthentication/CZ.AspNetCore.EasyAuthAuthentication.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
<ItemGroup>
5050
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
5151
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
52-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
53-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
54-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
55-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
52+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
53+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
54+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
55+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.2" />
5656
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
5757
</ItemGroup>
5858
</Project>

src/CZ.AspNetCore.EasyAuthAuthentication/EasyAuthAuthenticationHandler.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,39 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7979

8080
var authService = this.authenticationServices.FirstOrDefault(d => d.CanHandleAuthentification(this.Context));
8181
var enabledProviders = this.Options.ProviderOptions.Where(d => d.Enabled == true);
82-
if (authService != null && enabledProviders.Any(d => d.ProviderName == authService.GetType().Name))
82+
try
8383
{
84-
this.Logger.LogInformation($"use the {authService.GetType().Name} as auth handler.");
85-
return authService.AuthUser(this.Context, this.Options.ProviderOptions.FirstOrDefault(d => d.ProviderName == authService.GetType().Name));
86-
}
87-
else if (CanUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options))
88-
{
89-
var service = new LocalAuthMeService(this.Logger,
90-
this.Context.Request.Scheme,
91-
this.Context.Request.Host.ToString(),
92-
this.Context.Request.Cookies,
93-
this.Context.Request.Headers);
94-
return await service.AuthUser(this.Context, this.Options.LocalProviderOption);
95-
}
96-
else
97-
{
98-
if (IsContextUserNotAuthenticated(this.Context.User))
84+
if (authService != null && enabledProviders.Any(d => d.ProviderName == authService.GetType().Name))
9985
{
100-
this.Logger.LogInformation("The identity isn't set by easy auth.");
86+
this.Logger.LogInformation($"use the {authService.GetType().Name} as auth handler.");
87+
return authService.AuthUser(this.Context, this.Options.ProviderOptions.FirstOrDefault(d => d.ProviderName == authService.GetType().Name));
10188
}
102-
else
89+
else if (CanUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options))
10390
{
104-
this.Logger.LogInformation("identity already set, skipping middleware");
91+
var service = new LocalAuthMeService(this.Logger,
92+
this.Context.Request.Scheme,
93+
this.Context.Request.Host.ToString(),
94+
this.Context.Request.Cookies,
95+
this.Context.Request.Headers);
96+
return await service.AuthUser(this.Context, this.Options.LocalProviderOption);
10597
}
98+
else
99+
{
100+
if (IsContextUserNotAuthenticated(this.Context.User))
101+
{
102+
this.Logger.LogInformation("The identity isn't set by easy auth.");
103+
}
104+
else
105+
{
106+
this.Logger.LogInformation("identity already set, skipping middleware");
107+
}
106108

107-
return AuthenticateResult.NoResult();
109+
return AuthenticateResult.NoResult();
110+
}
111+
}
112+
catch (Exception ex)
113+
{
114+
return AuthenticateResult.Fail(ex);
108115
}
109116
}
110117
}

src/CZ.AspNetCore.EasyAuthAuthentication/Services/EasyAuthForAuthorizationTokenService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private AuthenticateResult AuthUser(HttpContext context, ProviderOptions? option
6666
/// <inheritdoc/>
6767
private bool CanHandleAuthentification(HttpContext httpContext) =>
6868
IsHeaderSet(httpContext.Request.Headers, AuthorizationHeader) &&
69-
httpContext.Request.Headers[AuthorizationHeader].FirstOrDefault().Contains(JWTIdentifier);
69+
httpContext.Request.Headers[AuthorizationHeader].FirstOrDefault()?.Contains(JWTIdentifier) == true;
7070

7171
private IEnumerable<AADClaimsModel> BuildFromAuthToken(JObject xMsClientPrincipal, ProviderOptions options)
7272
{

test/CZ.AspNetCore.EasyAuthAuthentication.Test/CZ.AspNetCore.EasyAuthAuthentication.Test.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
9-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
11-
<PackageReference Include="xunit" Version="2.4.2" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
8+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
9+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
11+
<PackageReference Include="xunit" Version="2.5.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>

0 commit comments

Comments
 (0)