|
| 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 MVC5Course.Models; |
| 14 | + |
| 15 | +namespace MVC5Course |
| 16 | +{ |
| 17 | + public class EmailService : IIdentityMessageService |
| 18 | + { |
| 19 | + public Task SendAsync(IdentityMessage message) |
| 20 | + { |
| 21 | + // 將您的電子郵件服務外掛到這裡以傳送電子郵件。 |
| 22 | + return Task.FromResult(0); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public class SmsService : IIdentityMessageService |
| 27 | + { |
| 28 | + public Task SendAsync(IdentityMessage message) |
| 29 | + { |
| 30 | + // 將您的 SMS 服務外掛到這裡以傳送簡訊。 |
| 31 | + return Task.FromResult(0); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // 設定此應用程式中使用的應用程式使用者管理員。UserManager 在 ASP.NET Identity 中定義且由應用程式中使用。 |
| 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 | + // 設定使用者名稱的驗證邏輯 |
| 47 | + manager.UserValidator = new UserValidator<ApplicationUser>(manager) |
| 48 | + { |
| 49 | + AllowOnlyAlphanumericUserNames = false, |
| 50 | + RequireUniqueEmail = true |
| 51 | + }; |
| 52 | + |
| 53 | + // 設定密碼的驗證邏輯 |
| 54 | + manager.PasswordValidator = new PasswordValidator |
| 55 | + { |
| 56 | + RequiredLength = 6, |
| 57 | + RequireNonLetterOrDigit = true, |
| 58 | + RequireDigit = true, |
| 59 | + RequireLowercase = true, |
| 60 | + RequireUppercase = true, |
| 61 | + }; |
| 62 | + |
| 63 | + // 設定使用者鎖定詳細資料 |
| 64 | + manager.UserLockoutEnabledByDefault = true; |
| 65 | + manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); |
| 66 | + manager.MaxFailedAccessAttemptsBeforeLockout = 5; |
| 67 | + |
| 68 | + // 註冊雙因素驗證提供者。此應用程式使用手機和電子郵件接收驗證碼以驗證使用者 |
| 69 | + // 您可以撰寫專屬提供者,並將它外掛到這裡。 |
| 70 | + manager.RegisterTwoFactorProvider("電話代碼", new PhoneNumberTokenProvider<ApplicationUser> |
| 71 | + { |
| 72 | + MessageFormat = "您的安全碼為 {0}" |
| 73 | + }); |
| 74 | + manager.RegisterTwoFactorProvider("電子郵件代碼", new EmailTokenProvider<ApplicationUser> |
| 75 | + { |
| 76 | + Subject = "安全碼", |
| 77 | + BodyFormat = "您的安全碼為 {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 | + // 設定在此應用程式中使用的應用程式登入管理員。 |
| 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 | +} |
0 commit comments