using System; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; using Microsoft.Owin.Security.DataProtection; using Microsoft.Owin.Security.Google; using Microsoft.Owin.Security.OAuth; using Owin; using Winsoft.GOV.XF.WX.Models; using Winsoft.GOV.XF.WX.Providers; namespace Winsoft.GOV.XF.WX { public partial class Startup { // 使应用程序可使用 OAuthAuthorization。然后,你便可以保护 Web API static Startup() { PublicClientId = "web"; OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), AuthorizeEndpointPath = new PathString("/Account/Authorize"), Provider = new ApplicationOAuthProvider(PublicClientId), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; } public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } public static string PublicClientId { get; private set; } // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864 public void ConfigureAuth(IAppBuilder app) { // 配置数据库上下文、用户管理器和登录管理器,以便为每个请求使用单个实例 app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationSignInManager.Create); // 使应用程序可使用 Cookie 存储登录用户的信息 app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { // 当用户登录时使应用程序可以验证安全戳。 // 这是一项安全功能,当你更改密码或者向帐户添加外部登录名时,将使用此功能。 OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(20), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); // 使用 Cookie 临时存储有关某个用户使用第三方登录提供程序进行登录的信息 app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // 使应用程序可以在双重身份验证过程中验证第二因素时暂时存储用户信息。 app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); // 使应用程序可以记住第二登录验证因素,例如电话或电子邮件。 // 选中此选项后,登录过程中执行的第二个验证步骤将保存到你登录时所在的设备上。 // 此选项类似于在登录时提供的“记住我”选项。 app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie); // 使应用程序可以使用不记名令牌来验证用户 app.UseOAuthBearerTokens(OAuthOptions); // 取消注释以下行可允许使用第三方登录提供程序登录 //app.UseMicrosoftAccountAuthentication( // clientId: "", // clientSecret: ""); //app.UseTwitterAuthentication( // consumerKey: "", // consumerSecret: ""); //app.UseFacebookAuthentication( // appId: "", // appSecret: ""); //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() //{ // ClientId = "", // ClientSecret = "" //}); } } }