2 using System.Collections.Generic;
3 using System.Data.Entity;
5 using System.Security.Claims;
6 using System.Threading.Tasks;
8 using Microsoft.AspNet.Identity;
9 using Microsoft.AspNet.Identity.EntityFramework;
10 using Microsoft.AspNet.Identity.Owin;
12 using Microsoft.Owin.Security;
13 using Projects.Models;
17 public class EmailService : IIdentityMessageService
19 public Task SendAsync(IdentityMessage message)
21 // Plug in your email service here to send an email.
22 return Task.FromResult(0);
26 public class SmsService : IIdentityMessageService
28 public Task SendAsync(IdentityMessage message)
30 // Plug in your SMS service here to send a text message.
31 return Task.FromResult(0);
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>
38 public ApplicationUserManager(IUserStore<ApplicationUser> store)
43 public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
45 var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
46 // Configure validation logic for usernames
47 manager.UserValidator = new UserValidator<ApplicationUser>(manager)
49 AllowOnlyAlphanumericUserNames = false,
50 RequireUniqueEmail = true
53 // Configure validation logic for passwords
54 manager.PasswordValidator = new PasswordValidator
57 RequireNonLetterOrDigit = true,
59 RequireLowercase = true,
60 RequireUppercase = true,
63 // Configure user lockout defaults
64 manager.UserLockoutEnabledByDefault = true;
65 manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
66 manager.MaxFailedAccessAttemptsBeforeLockout = 5;
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>
72 MessageFormat = "Your security code is {0}"
74 manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
76 Subject = "Security Code",
77 BodyFormat = "Your security code is {0}"
79 manager.EmailService = new EmailService();
80 manager.SmsService = new SmsService();
81 var dataProtectionProvider = options.DataProtectionProvider;
82 if (dataProtectionProvider != null)
84 manager.UserTokenProvider =
85 new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
91 // Configure the application sign-in manager which is used in this application.
92 public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
94 public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
95 : base(userManager, authenticationManager)
99 public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
101 return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
104 public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
106 return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);