From f473c64540b148a784b71f180c9f1b9bf8bd0d2b Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Fri, 23 Sep 2016 21:23:33 -0400 Subject: [PATCH] Configure tolken provider --- InventoryTraker.Web/App_Start/Startup.cs | 6 +++++- InventoryTraker.Web/Controllers/UserController.cs | 5 ----- .../Identity/ApplicationUserManager.cs | 14 +++++++++++++- InventoryTraker.Web/Migrations/SeedData.cs | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/InventoryTraker.Web/App_Start/Startup.cs b/InventoryTraker.Web/App_Start/Startup.cs index 433a36d..a082429 100644 --- a/InventoryTraker.Web/App_Start/Startup.cs +++ b/InventoryTraker.Web/App_Start/Startup.cs @@ -1,6 +1,8 @@ -using Microsoft.AspNet.Identity; +using Heroic.Web.IoC; +using Microsoft.AspNet.Identity; using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; +using Microsoft.Owin.Security.DataProtection; using Owin; namespace InventoryTraker.Web @@ -9,6 +11,8 @@ namespace InventoryTraker.Web { public void Configuration(IAppBuilder app) { + IoC.Container.Inject(app.GetDataProtectionProvider()); + var authenticationOptions = new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, diff --git a/InventoryTraker.Web/Controllers/UserController.cs b/InventoryTraker.Web/Controllers/UserController.cs index 629d1c6..d3018bc 100644 --- a/InventoryTraker.Web/Controllers/UserController.cs +++ b/InventoryTraker.Web/Controllers/UserController.cs @@ -8,8 +8,6 @@ using InventoryTraker.Web.Core; using InventoryTraker.Web.Identity; using InventoryTraker.Web.Models; using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Identity.Owin; -using Microsoft.Owin.Security.DataProtection; namespace InventoryTraker.Web.Controllers { @@ -74,9 +72,6 @@ namespace InventoryTraker.Web.Controllers if (!string.IsNullOrEmpty(form.Password)) { - var provider = new DpapiDataProtectionProvider("Inventory Traker"); - _userManager.UserTokenProvider = new DataProtectorTokenProvider( - provider.Create("EmailConfirmation")); var resetToken = await _userManager.GeneratePasswordResetTokenAsync(user.Id); var resetResult = await _userManager.ResetPasswordAsync(user.Id, resetToken, form.Password); if (!resetResult.Succeeded) diff --git a/InventoryTraker.Web/Identity/ApplicationUserManager.cs b/InventoryTraker.Web/Identity/ApplicationUserManager.cs index 0336fed..a44ac14 100644 --- a/InventoryTraker.Web/Identity/ApplicationUserManager.cs +++ b/InventoryTraker.Web/Identity/ApplicationUserManager.cs @@ -1,11 +1,14 @@ +using System; using InventoryTraker.Web.Core; using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Identity.Owin; +using Microsoft.Owin.Security.DataProtection; namespace InventoryTraker.Web.Identity { public class ApplicationUserManager : UserManager { - public ApplicationUserManager(IUserStore store) + public ApplicationUserManager(IUserStore store, IDataProtectionProvider dataProtectionProvider) : base(store) { UserValidator = new UserValidator(this) @@ -13,6 +16,15 @@ namespace InventoryTraker.Web.Identity AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; + + if (dataProtectionProvider != null) + { + var dataProtector = dataProtectionProvider.Create("Protector"); + UserTokenProvider = new DataProtectorTokenProvider(dataProtector) + { + TokenLifespan = TimeSpan.FromHours(1), + }; + } } } } \ No newline at end of file diff --git a/InventoryTraker.Web/Migrations/SeedData.cs b/InventoryTraker.Web/Migrations/SeedData.cs index 6bae757..45a9a4b 100644 --- a/InventoryTraker.Web/Migrations/SeedData.cs +++ b/InventoryTraker.Web/Migrations/SeedData.cs @@ -24,7 +24,7 @@ namespace InventoryTraker.Web.Migrations { if (!context.Users.Any()) { - var manager = new ApplicationUserManager(new UserStore(context)); + var manager = new ApplicationUserManager(new UserStore(context), null); manager.Create(new User { Email = "james.kolpack@gmail.com",