Created
August 2, 2016 06:23
-
-
Save esergueev/82eb47dfb731bba39e0ae9a696477fae to your computer and use it in GitHub Desktop.
Code review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseCookieAuthentication(new CookieAuthenticationOptions | |
{ | |
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, | |
ExpireTimeSpan = TimeSpan.FromDays(30), | |
SlidingExpiration = true, | |
Provider = new CookieAuthenticationProvider | |
{ | |
OnValidateIdentity = async cookieCtx => | |
{ | |
var validator = DependencyResolver.Current.GetService<IAuthenticationSessionValidator>(); | |
var isValid = false; | |
if (cookieCtx.Properties.IssuedUtc.HasValue) | |
{ | |
cookieCtx.Identity.AddClaim(new Claim(Constants.ClaimTypes.AuthenticationTime, | |
cookieCtx.Properties.IssuedUtc.Value.ToEpochTime().ToString())); | |
isValid = | |
await | |
validator.IsAuthenticationSessionValidAsync(new ClaimsPrincipal(cookieCtx.Identity)); | |
} | |
if (isValid == false) | |
{ | |
cookieCtx.RejectIdentity(); | |
} | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataEntryAuthenticationSessionValidator : IAuthenticationAppSessionValidator | |
{ | |
private readonly IMediator _mediator; | |
public DataEntryAuthenticationSessionValidator(IMediator mediator) | |
{ | |
_mediator = mediator; | |
} | |
public Task<bool> IsAuthenticationSessionValidAsync(CookieValidateIdentityContext ctx) | |
{ | |
var date = _mediator.Send(new ReadRecordQuery<PractitionerRecord>(ctx.Identity.GetSubjectId())).Updated; | |
return | |
Task.FromResult(ctx.Properties.IssuedUtc.HasValue && | |
DateTimeOffset.Compare(new DateTimeOffset(date), ctx.Properties.IssuedUtc.Value) < 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment