-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
113 lines (102 loc) · 3.46 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Data.Entity;
using System.Web;
using Castle.MicroKernel.Registration;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
[assembly: OwinStartup(typeof(Startup))]
namespace WebApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
private void ConfigureAuth(IAppBuilder app)
{
var container = DIContainer.GetConfiguredContainer();
container.Register(
Component
.For<IAppBuilder>()
.Instance(app),
Component
.For<ApplicationDbContext>()
.DependsOn(Dependency.OnValue<string>("DefaultConnection"))
.LifestyleTransient(),
Component
.For<IUserStore<WebAppUser>>()
.ImplementedBy<UserStore<WebAppUser>>()
.DependsOn(Dependency.OnComponent<DbContext, ApplicationDbContext>())
.LifestyleTransient(),
Component
.For<ApplicationUserManager>()
.UsingFactoryMethod(kernel =>
CreateCustomUserManager(
kernel.Resolve<IUserStore<WebAppUser>>(),
kernel.Resolve<IAppBuilder>()))
.LifestyleTransient(),
Component
.For<IRoleStore<IdentityRole, string>>()
.ImplementedBy<RoleStore<IdentityRole>>()
.DependsOn(Dependency.OnComponent<DbContext, ApplicationDbContext>())
.LifestyleTransient(),
Component
.For<ApplicationRoleManager>()
.LifestyleTransient(),
Component
.For<IAuthenticationManager>()
.UsingFactoryMethod(kernel => HttpContext.Current.GetOwinContext().Authentication)
.LifestyleTransient(),
Component
.For<ApplicationSignInManager>()
.LifestyleTransient());
app.CreatePerOwinContext(() => container.Resolve<ApplicationUserManager>());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, WebAppUser>(
validateInterval: TimeSpan.FromMinutes(15),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
}
private static ApplicationUserManager CreateCustomUserManager(
IUserStore<WebAppUser> store,
IAppBuilder appBuilder)
{
var manager = new ApplicationUserManager(store);
manager.UserValidator = new UserValidator<WebAppUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = Common.Constants.MinimumUserPasswordLength,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false
};
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = Common.Constants.AccountLockoutTimeSpan;
manager.MaxFailedAccessAttemptsBeforeLockout = Common.Constants.MaxFailedAccessAttemptsBeforeLockout;
var dataProtectionProvider = appBuilder.GetDataProtectionProvider();
if (dataProtectionProvider != null)
{
var dataProtector = dataProtectionProvider.Create("ASP.NET Identity");
manager.UserTokenProvider = new DataProtectorTokenProvider<WebAppUser>(dataProtector);
}
return manager;
}
}
}