React to DI change for GetService

This commit is contained in:
Hao Kung 2014-10-16 15:33:59 -07:00
parent 06c64826ee
commit 0df5dfd36b
2 changed files with 10 additions and 4 deletions

View File

@ -55,17 +55,22 @@ namespace Microsoft.AspNet.Identity
}
var issuedUtc = context.Properties.IssuedUtc;
if (context.HttpContext.RequestServices == null)
{
throw new InvalidOperationException("TODO: RequestServices is null, missing Use[Request]Services?");
}
// Only validate if enough time has elapsed
var validate = (issuedUtc == null);
if (issuedUtc != null)
{
var timeElapsed = currentUtc.Subtract(issuedUtc.Value);
var identityOptions = context.HttpContext.RequestServices.GetService<IOptions<IdentityOptions>>().Options;
validate = timeElapsed > identityOptions.SecurityStampValidationInterval;
var accessor = context.HttpContext.RequestServices.GetRequiredService<IOptions<IdentityOptions>>();
validate = timeElapsed > accessor.Options.SecurityStampValidationInterval;
}
if (validate)
{
var validator = context.HttpContext.RequestServices.GetService<ISecurityStampValidator>();
var validator = context.HttpContext.RequestServices.GetRequiredService<ISecurityStampValidator>();
return validator.Validate(context, context.Identity);
}
return Task.FromResult(0);

View File

@ -27,7 +27,8 @@ namespace Microsoft.AspNet.Identity.Test
var id = new ClaimsIdentity(IdentityOptions.ApplicationCookieAuthenticationType);
var ticket = new AuthenticationTicket(id, new AuthenticationProperties { IssuedUtc = DateTimeOffset.UtcNow });
var context = new CookieValidateIdentityContext(httpContext.Object, ticket, new CookieAuthenticationOptions());
await Assert.ThrowsAsync<Exception>(() => SecurityStampValidator.ValidateIdentityAsync(context));
var ex = await Assert.ThrowsAsync<Exception>(() => SecurityStampValidator.ValidateIdentityAsync(context));
Assert.True(ex.Message.Contains("No service for type 'Microsoft.Framework.OptionsModel.IOptions"));
}
[Theory]