【问题标题】:ASP.NET web api token expiration time did not work on serverASP.NET web api 令牌过期时间在服务器上不起作用
【发布时间】:2017-06-04 06:40:09
【问题描述】:

我将令牌的到期时间设置为 200 天。它在本地运行良好,但是在我将其上传到服务器后,它似乎只是默认为 20 分钟。

 OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new 
                     PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(200),
            // In production mode set AllowInsecureHttp = false
            AllowInsecureHttp = true
        };

【问题讨论】:

    标签: asp.net asp.net-web-api token


    【解决方案1】:

    为新的 OAuthAuthorizationServerOptions 使用变量

      public void ConfigureOAuth(IAppBuilder app)
        {
            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {   AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                Provider = new SimpleAuthorizationServerProvider()
            };
    

    然后使用生成器

    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码

      public partial class Startup
      {
          public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
      
          public static string PublicClientId { get; private set; }
      
          // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
          public void ConfigureAuth(IAppBuilder app)
          {
              // Configure the db context and user manager to use a single instance per request
              app.CreatePerOwinContext(ApplicationDbContext.Create);
              app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
      
              // Enable the application to use a cookie to store information for the signed in user
              // and to use a cookie to temporarily store information about a user logging in with a third party login provider
              app.UseCookieAuthentication(new CookieAuthenticationOptions());
              app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
      
              // Configure the application for OAuth based flow
              PublicClientId = "self";
              OAuthOptions = new OAuthAuthorizationServerOptions
              {
                  TokenEndpointPath = new PathString("/Token"),
                  Provider = new ApplicationOAuthProvider(PublicClientId),
                  AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                  AccessTokenExpireTimeSpan = TimeSpan.FromDays(200),
                  AllowInsecureHttp = true,
              };
      
              // Enable the application to use bearer tokens to authenticate users
              app.UseOAuthBearerTokens(OAuthOptions);
                     }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        • 2019-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多