华恒Mes鼎捷代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.7 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNet.Identity;
  5. using Microsoft.AspNet.Identity.EntityFramework;
  6. using Microsoft.Owin;
  7. using Microsoft.Owin.Security.Cookies;
  8. using Microsoft.Owin.Security.Google;
  9. using Microsoft.Owin.Security.OAuth;
  10. using Owin;
  11. using WebApplication1.Providers;
  12. using WebApplication1.Models;
  13. namespace WebApplication1
  14. {
  15. public partial class Startup
  16. {
  17. public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
  18. public static string PublicClientId { get; private set; }
  19. // 有关配置身份验证的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=301864
  20. public void ConfigureAuth(IAppBuilder app)
  21. {
  22. // 将数据库上下文和用户管理器配置为对每个请求使用单个实例
  23. app.CreatePerOwinContext(ApplicationDbContext.Create);
  24. app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
  25. // 使应用程序可以使用 Cookie 来存储已登录用户的信息
  26. // 并使用 Cookie 来临时存储有关使用第三方登录提供程序登录的用户的信息
  27. app.UseCookieAuthentication(new CookieAuthenticationOptions());
  28. app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  29. // 针对基于 OAuth 的流配置应用程序
  30. PublicClientId = "self";
  31. OAuthOptions = new OAuthAuthorizationServerOptions
  32. {
  33. TokenEndpointPath = new PathString("/Token"),
  34. Provider = new ApplicationOAuthProvider(PublicClientId),
  35. AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
  36. AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
  37. //在生产模式下设 AllowInsecureHttp = false
  38. AllowInsecureHttp = true
  39. };
  40. // 使应用程序可以使用不记名令牌来验证用户身份
  41. app.UseOAuthBearerTokens(OAuthOptions);
  42. // 取消注释以下行可允许使用第三方登录提供程序登录
  43. //app.UseMicrosoftAccountAuthentication(
  44. // clientId: "",
  45. // clientSecret: "");
  46. //app.UseTwitterAuthentication(
  47. // consumerKey: "",
  48. // consumerSecret: "");
  49. //app.UseFacebookAuthentication(
  50. // appId: "",
  51. // appSecret: "");
  52. //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
  53. //{
  54. // ClientId = "",
  55. // ClientSecret = ""
  56. //});
  57. }
  58. }
  59. }