Configure tolken provider

This commit is contained in:
2016-09-23 21:23:33 -04:00
parent 0b5dde065a
commit f473c64540
4 changed files with 19 additions and 8 deletions
+5 -1
View File
@@ -1,6 +1,8 @@
using Microsoft.AspNet.Identity; using Heroic.Web.IoC;
using Microsoft.AspNet.Identity;
using Microsoft.Owin; using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies; using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Owin; using Owin;
namespace InventoryTraker.Web namespace InventoryTraker.Web
@@ -9,6 +11,8 @@ namespace InventoryTraker.Web
{ {
public void Configuration(IAppBuilder app) public void Configuration(IAppBuilder app)
{ {
IoC.Container.Inject(app.GetDataProtectionProvider());
var authenticationOptions = new CookieAuthenticationOptions var authenticationOptions = new CookieAuthenticationOptions
{ {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
@@ -8,8 +8,6 @@ using InventoryTraker.Web.Core;
using InventoryTraker.Web.Identity; using InventoryTraker.Web.Identity;
using InventoryTraker.Web.Models; using InventoryTraker.Web.Models;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;
namespace InventoryTraker.Web.Controllers namespace InventoryTraker.Web.Controllers
{ {
@@ -74,9 +72,6 @@ namespace InventoryTraker.Web.Controllers
if (!string.IsNullOrEmpty(form.Password)) if (!string.IsNullOrEmpty(form.Password))
{ {
var provider = new DpapiDataProtectionProvider("Inventory Traker");
_userManager.UserTokenProvider = new DataProtectorTokenProvider<User>(
provider.Create("EmailConfirmation"));
var resetToken = await _userManager.GeneratePasswordResetTokenAsync(user.Id); var resetToken = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
var resetResult = await _userManager.ResetPasswordAsync(user.Id, resetToken, form.Password); var resetResult = await _userManager.ResetPasswordAsync(user.Id, resetToken, form.Password);
if (!resetResult.Succeeded) if (!resetResult.Succeeded)
@@ -1,11 +1,14 @@
using System;
using InventoryTraker.Web.Core; using InventoryTraker.Web.Core;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;
namespace InventoryTraker.Web.Identity namespace InventoryTraker.Web.Identity
{ {
public class ApplicationUserManager : UserManager<User> public class ApplicationUserManager : UserManager<User>
{ {
public ApplicationUserManager(IUserStore<User> store) public ApplicationUserManager(IUserStore<User> store, IDataProtectionProvider dataProtectionProvider)
: base(store) : base(store)
{ {
UserValidator = new UserValidator<User>(this) UserValidator = new UserValidator<User>(this)
@@ -13,6 +16,15 @@ namespace InventoryTraker.Web.Identity
AllowOnlyAlphanumericUserNames = false, AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true RequireUniqueEmail = true
}; };
if (dataProtectionProvider != null)
{
var dataProtector = dataProtectionProvider.Create("Protector");
UserTokenProvider = new DataProtectorTokenProvider<User, string>(dataProtector)
{
TokenLifespan = TimeSpan.FromHours(1),
};
}
} }
} }
} }
+1 -1
View File
@@ -24,7 +24,7 @@ namespace InventoryTraker.Web.Migrations
{ {
if (!context.Users.Any()) if (!context.Users.Any())
{ {
var manager = new ApplicationUserManager(new UserStore<User>(context)); var manager = new ApplicationUserManager(new UserStore<User>(context), null);
manager.Create(new User manager.Create(new User
{ {
Email = "james.kolpack@gmail.com", Email = "james.kolpack@gmail.com",