IdentityConfig.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Winsoft.GOV.XF.WX.Models;
  14. namespace Winsoft.GOV.XF.WX
  15. {
  16. public class EmailService : IIdentityMessageService
  17. {
  18. public Task SendAsync(IdentityMessage message)
  19. {
  20. // 在此处插入电子邮件服务可发送电子邮件。
  21. return Task.FromResult(0);
  22. }
  23. }
  24. public class SmsService : IIdentityMessageService
  25. {
  26. public Task SendAsync(IdentityMessage message)
  27. {
  28. // 在此处插入 SMS 服务可发送短信。
  29. return Task.FromResult(0);
  30. }
  31. }
  32. // 配置要在此应用程序中使用的应用程序用户管理器。
  33. public class ApplicationUserManager : UserManager<ApplicationUser>
  34. {
  35. public ApplicationUserManager(IUserStore<ApplicationUser> store)
  36. : base(store)
  37. {
  38. }
  39. public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
  40. IOwinContext context)
  41. {
  42. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
  43. // 配置用户名的验证逻辑
  44. manager.UserValidator = new UserValidator<ApplicationUser>(manager)
  45. {
  46. AllowOnlyAlphanumericUserNames = false,
  47. RequireUniqueEmail = true
  48. };
  49. // 配置密码的验证逻辑
  50. manager.PasswordValidator = new PasswordValidator
  51. {
  52. RequiredLength = 6,
  53. RequireNonLetterOrDigit = true,
  54. RequireDigit = true,
  55. RequireLowercase = true,
  56. RequireUppercase = true,
  57. };
  58. // 配置用户锁定默认值
  59. manager.UserLockoutEnabledByDefault = true;
  60. manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
  61. manager.MaxFailedAccessAttemptsBeforeLockout = 5;
  62. // 注册双重身份验证提供程序。此应用程序使用手机和电子邮件作为接收用于验证用户的代码的一个步骤
  63. // 你可以编写自己的提供程序并将其插入到此处。
  64. manager.RegisterTwoFactorProvider("电话代码", new PhoneNumberTokenProvider<ApplicationUser>
  65. {
  66. MessageFormat = "你的安全代码是 {0}"
  67. });
  68. manager.RegisterTwoFactorProvider("电子邮件代码", new EmailTokenProvider<ApplicationUser>
  69. {
  70. Subject = "安全代码",
  71. BodyFormat = "你的安全代码是 {0}"
  72. });
  73. manager.EmailService = new EmailService();
  74. manager.SmsService = new SmsService();
  75. var dataProtectionProvider = options.DataProtectionProvider;
  76. if (dataProtectionProvider != null)
  77. {
  78. manager.UserTokenProvider =
  79. new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
  80. }
  81. return manager;
  82. }
  83. }
  84. // 配置要在此应用程序中使用的应用程序登录管理器。
  85. public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
  86. {
  87. public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
  88. base(userManager, authenticationManager)
  89. { }
  90. public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
  91. {
  92. return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
  93. }
  94. public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
  95. {
  96. return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
  97. }
  98. }
  99. }