【问题标题】:Multiple Token lifetimes in WebAPI2 with asp.net Identity具有 asp.net 身份的 WebAPI2 中的多个令牌生命周期
【发布时间】:2014-04-25 21:10:39
【问题描述】:

我在我的 WebAPI 应用程序中使用带有 OAuth 的 OWIN 中间件的 asp.net 身份提供程序。使用模板和

https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

我在我的 WebAPI 端点上使用了 OAuth。但是,我没有看到如何扩展这个架构来为不同的请求提供不同的令牌生命周期。

例如,我的 REST API 将被 Web 应用程序和移动应用程序使用。我希望移动应用程序的令牌生命周期比 Web 应用程序长得多。

在我的 Startup.Auth.cs 文件中,我看到以下 OAuth 配置-

        // Enable the application to use bearer tokens to authenticate users
        app.UseOAuthBearerTokens(new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider<ApplicationUserManager, DirectoryUser, Guid>(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true
        });

有没有办法覆盖每个令牌请求的这种行为?例如,我可以公开一个 “/Token” -> 14 天和“/DeviceToken” -> 60 天。这可能吗?

【问题讨论】:

    标签: asp.net oauth-2.0 asp.net-identity owin asp.net-web-api2


    【解决方案1】:

    我能够通过将以下内容插入示例中的 OAuth 提供程序 (ApplicationOAuthProvider.cs) 来解决此问题-

       public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.Get<TUserManager>();
    
            TUser user = await userManager.FindAsync(context.UserName, context.Password);
    
    
            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
    
    
            //if user, expire 60. If Admin, 14 days
            if (userManager.IsInRole(user.Id, "Users"))
            {
                context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromDays(60);
            }
            else {
                context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromDays(14);
            }
    
            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
    context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
    
    
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
    
        }
    

    【讨论】:

    • 感谢您用答案更新您的问题。我会在不久的将来做类似的事情,你的问题答案会派上用场。
    • 没问题,我会保持开放一天左右,以防有人有更好的解决方案,然后我会将其标记为已回答
    • context.Options 是您的OAuth configuration 中对OAuthAuthorizationServerOptions 的引用。因此,当您更改 GrantResourceOwnerCredentials 中的选项时,这意味着您将全局更改 AccessTokenExpireTimeSpan。所以我更喜欢 dichen 的答案作为最佳答案。
    【解决方案2】:

    设置 context.Options.AccessTokenExpireTimeSpan 实际上会改变全局值,并影响所有请求,这对原始要求不起作用。

    正确的地方是 TokenEndpoint 方法。

    public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        ...
    
        if (isRequestFromDevice)
        {
            context.Properties.ExpiresUtc = DateTime.UtcNow.AddDays(60);
        }
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 2018-12-23
      • 2019-06-17
      • 1970-01-01
      • 2011-07-09
      • 2016-05-15
      • 2020-03-28
      • 2017-07-05
      • 2015-01-08
      相关资源
      最近更新 更多