Startup.Auth.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using Microsoft.AspNet.Identity;
  3. using Microsoft.AspNet.Identity.EntityFramework;
  4. using Microsoft.AspNet.Identity.Owin;
  5. using Microsoft.Owin;
  6. using Microsoft.Owin.Security.Cookies;
  7. using Microsoft.Owin.Security.DataProtection;
  8. using Microsoft.Owin.Security.Google;
  9. using Microsoft.Owin.Security.OAuth;
  10. using Owin;
  11. using Winsoft.GOV.XF.WX.Models;
  12. using Winsoft.GOV.XF.WX.Providers;
  13. namespace Winsoft.GOV.XF.WX
  14. {
  15. public partial class Startup
  16. {
  17. // 使应用程序可使用 OAuthAuthorization。然后,你便可以保护 Web API
  18. static Startup()
  19. {
  20. PublicClientId = "web";
  21. OAuthOptions = new OAuthAuthorizationServerOptions
  22. {
  23. TokenEndpointPath = new PathString("/Token"),
  24. AuthorizeEndpointPath = new PathString("/Account/Authorize"),
  25. Provider = new ApplicationOAuthProvider(PublicClientId),
  26. AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
  27. AllowInsecureHttp = true
  28. };
  29. }
  30. public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
  31. public static string PublicClientId { get; private set; }
  32. // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
  33. public void ConfigureAuth(IAppBuilder app)
  34. {
  35. // 配置数据库上下文、用户管理器和登录管理器,以便为每个请求使用单个实例
  36. app.CreatePerOwinContext(ApplicationDbContext.Create);
  37. app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
  38. app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
  39. // 使应用程序可使用 Cookie 存储登录用户的信息
  40. app.UseCookieAuthentication(new CookieAuthenticationOptions
  41. {
  42. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  43. LoginPath = new PathString("/Account/Login"),
  44. Provider = new CookieAuthenticationProvider
  45. {
  46. // 当用户登录时使应用程序可以验证安全戳。
  47. // 这是一项安全功能,当你更改密码或者向帐户添加外部登录名时,将使用此功能。
  48. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
  49. validateInterval: TimeSpan.FromMinutes(20),
  50. regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
  51. }
  52. });
  53. // 使用 Cookie 临时存储有关某个用户使用第三方登录提供程序进行登录的信息
  54. app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  55. // 使应用程序可以在双重身份验证过程中验证第二因素时暂时存储用户信息。
  56. app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
  57. // 使应用程序可以记住第二登录验证因素,例如电话或电子邮件。
  58. // 选中此选项后,登录过程中执行的第二个验证步骤将保存到你登录时所在的设备上。
  59. // 此选项类似于在登录时提供的“记住我”选项。
  60. app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
  61. // 使应用程序可以使用不记名令牌来验证用户
  62. app.UseOAuthBearerTokens(OAuthOptions);
  63. // 取消注释以下行可允许使用第三方登录提供程序登录
  64. //app.UseMicrosoftAccountAuthentication(
  65. // clientId: "",
  66. // clientSecret: "");
  67. //app.UseTwitterAuthentication(
  68. // consumerKey: "",
  69. // consumerSecret: "");
  70. //app.UseFacebookAuthentication(
  71. // appId: "",
  72. // appSecret: "");
  73. //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
  74. //{
  75. // ClientId = "",
  76. // ClientSecret = ""
  77. //});
  78. }
  79. }
  80. }