mirror of https://github.com/abpframework/abp.git
7 changed files with 121 additions and 6 deletions
@ -0,0 +1,35 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authentication; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Security.Claims; |
||||
|
|
||||
|
public class AbpClaimsTransformation : IClaimsTransformation |
||||
|
{ |
||||
|
protected IOptions<AbpClaimsMapOptions> AbpClaimsMapOptions { get; } |
||||
|
|
||||
|
public AbpClaimsTransformation(IOptions<AbpClaimsMapOptions> abpClaimsMapOptions) |
||||
|
{ |
||||
|
AbpClaimsMapOptions = abpClaimsMapOptions; |
||||
|
} |
||||
|
|
||||
|
public virtual Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) |
||||
|
{ |
||||
|
var mapClaims = principal.Claims.Where(claim => AbpClaimsMapOptions.Value.Maps.Keys.Contains(claim.Type)); |
||||
|
|
||||
|
principal.AddIdentity(new ClaimsIdentity(mapClaims.Select( |
||||
|
claim => new Claim( |
||||
|
AbpClaimsMapOptions.Value.Maps[claim.Type](), |
||||
|
claim.Value, |
||||
|
claim.ValueType, |
||||
|
claim.Issuer |
||||
|
) |
||||
|
) |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
return Task.FromResult(principal); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
using System; |
||||
|
using System.Security.Claims; |
||||
|
using System.Text.Encodings.Web; |
||||
|
using System.Threading.Tasks; |
||||
|
using DeviceDetectorNET; |
||||
|
using Microsoft.AspNetCore.Authentication; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc; |
||||
|
|
||||
|
public static class FakeAuthenticationSchemeDefaults |
||||
|
{ |
||||
|
public static string Scheme => "FakeAuthenticationScheme"; |
||||
|
} |
||||
|
|
||||
|
public static class FakeAuthenticationBuilderExtensions |
||||
|
{ |
||||
|
public static AuthenticationBuilder AddFakeAuthentication(this AuthenticationBuilder builder) |
||||
|
{ |
||||
|
return builder.AddScheme<FakeAuthenticationOptions, FakeAuthenticationHandler>(FakeAuthenticationSchemeDefaults.Scheme, _ => { }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class FakeAuthenticationOptions : AuthenticationSchemeOptions |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class FakeAuthenticationHandler : AuthenticationHandler<FakeAuthenticationOptions> |
||||
|
{ |
||||
|
private readonly FakeUserClaims _fakeUserClaims; |
||||
|
|
||||
|
[Obsolete] |
||||
|
public FakeAuthenticationHandler( |
||||
|
IOptionsMonitor<FakeAuthenticationOptions> options, |
||||
|
ILoggerFactory logger, |
||||
|
UrlEncoder encoder, |
||||
|
ISystemClock clock, |
||||
|
FakeUserClaims fakeUserClaims) : base(options, logger, encoder, clock) |
||||
|
{ |
||||
|
_fakeUserClaims = fakeUserClaims; |
||||
|
} |
||||
|
|
||||
|
public FakeAuthenticationHandler( |
||||
|
IOptionsMonitor<FakeAuthenticationOptions> options, |
||||
|
ILoggerFactory logger, |
||||
|
UrlEncoder encoder, |
||||
|
FakeUserClaims fakeUserClaims) |
||||
|
: base(options, logger, encoder) |
||||
|
{ |
||||
|
_fakeUserClaims = fakeUserClaims; |
||||
|
} |
||||
|
|
||||
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync() |
||||
|
{ |
||||
|
if (_fakeUserClaims.Claims.Any()) |
||||
|
{ |
||||
|
return Task.FromResult(AuthenticateResult.Success( |
||||
|
new AuthenticationTicket( |
||||
|
new ClaimsPrincipal(new ClaimsIdentity(_fakeUserClaims.Claims, |
||||
|
FakeAuthenticationSchemeDefaults.Scheme)), |
||||
|
FakeAuthenticationSchemeDefaults.Scheme))); |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(AuthenticateResult.NoResult()); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue