From 0df5dfd36b35bcde7165d5f4484551e9bf14a366 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 16 Oct 2014 15:33:59 -0700 Subject: [PATCH] React to DI change for GetService --- .../SecurityStampValidator.cs | 11 ++++++++--- .../SecurityStampValidatorTest.cs | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs index c9baa4cf4b..9fc9d16af9 100644 --- a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs +++ b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs @@ -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>().Options; - validate = timeElapsed > identityOptions.SecurityStampValidationInterval; + var accessor = context.HttpContext.RequestServices.GetRequiredService>(); + validate = timeElapsed > accessor.Options.SecurityStampValidationInterval; } if (validate) { - var validator = context.HttpContext.RequestServices.GetService(); + var validator = context.HttpContext.RequestServices.GetRequiredService(); return validator.Validate(context, context.Identity); } return Task.FromResult(0); diff --git a/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs b/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs index 96cf3cc0cb..29bbe58885 100644 --- a/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs @@ -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(() => SecurityStampValidator.ValidateIdentityAsync(context)); + var ex = await Assert.ThrowsAsync(() => SecurityStampValidator.ValidateIdentityAsync(context)); + Assert.True(ex.Message.Contains("No service for type 'Microsoft.Framework.OptionsModel.IOptions")); } [Theory]