initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / App_Start / IdentityConfig.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Data.Entity;
4 using System.Linq;
5 using System.Security.Claims;
6 using System.Threading.Tasks;
7 using System.Web;
8 using Microsoft.AspNet.Identity;
9 using Microsoft.AspNet.Identity.EntityFramework;
10 using Microsoft.AspNet.Identity.Owin;
11 using Microsoft.Owin;
12 using Microsoft.Owin.Security;
13 using CPE.App.Web.Models;
14
15 namespace CPE.App.Web
16 {
17     public class EmailService : IIdentityMessageService
18     {
19         public Task SendAsync(IdentityMessage message)
20         {
21             // Plug in your email service here to send an email.
22             return Task.FromResult(0);
23         }
24     }
25
26     public class SmsService : IIdentityMessageService
27     {
28         public Task SendAsync(IdentityMessage message)
29         {
30             // Plug in your SMS service here to send a text message.
31             return Task.FromResult(0);
32         }
33     }
34
35     // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
36     public class ApplicationUserManager : UserManager<ApplicationUser>
37     {
38         public ApplicationUserManager(IUserStore<ApplicationUser> store)
39             : base(store)
40         {
41         }
42
43         public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
44         {
45             var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
46             // Configure validation logic for usernames
47             manager.UserValidator = new UserValidator<ApplicationUser>(manager)
48             {
49                 AllowOnlyAlphanumericUserNames = false,
50                 RequireUniqueEmail = true
51             };
52
53             // Configure validation logic for passwords
54             manager.PasswordValidator = new PasswordValidator
55             {
56                 RequiredLength = 6,
57                 RequireNonLetterOrDigit = true,
58                 RequireDigit = true,
59                 RequireLowercase = true,
60                 RequireUppercase = true,
61             };
62
63             // Configure user lockout defaults
64             manager.UserLockoutEnabledByDefault = true;
65             manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
66             manager.MaxFailedAccessAttemptsBeforeLockout = 5;
67
68             // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
69             // You can write your own provider and plug it in here.
70             manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
71             {
72                 MessageFormat = "Your security code is {0}"
73             });
74             manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
75             {
76                 Subject = "Security Code",
77                 BodyFormat = "Your security code is {0}"
78             });
79             manager.EmailService = new EmailService();
80             manager.SmsService = new SmsService();
81             var dataProtectionProvider = options.DataProtectionProvider;
82             if (dataProtectionProvider != null)
83             {
84                 manager.UserTokenProvider = 
85                     new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
86             }
87             return manager;
88         }
89     }
90
91     // Configure the application sign-in manager which is used in this application.
92     public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
93     {
94         public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
95             : base(userManager, authenticationManager)
96         {
97         }
98
99         public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
100         {
101             return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
102         }
103
104         public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
105         {
106             return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
107         }
108     }
109 }