asp.net-web-api - 存储owin oauth承载 token

标签 asp.net-web-api oauth owin

我正在使用默认的owin oauth服务器创建一个简单的身份验证服务器。提供正确的凭证后,将生成一个承载 token 并将其返回给客户端。我在Taiseer中使用了这个tutorial

我想在将 token 发送到客户端之前将 token 存储在数据库中。
也许我完全忽略了它,但是在发送 token 之前我可以在哪里获得 token 呢?据我所知, token 是在GrantResourceOwnerCredentials方法中验证票证后生成的。
我猜 token 存储在上下文中。我怎样才能把它弄出来?

Startup.cs

private void ConfigureAuthServer(IAppBuilder app) {
  // Configure the application for OAuth based flow
  var oAuthServerOptions = new OAuthAuthorizationServerOptions {
    //For Dev enviroment only (on production should be AllowInsecureHttp = false)
    AllowInsecureHttp = true,
    TokenEndpointPath = new PathString("/oauth/token"),
    Provider = new ApplicationOAuthProvider(),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14)
  };

  // Enable the application to use bearer tokens to authenticate users
  app.UseOAuthAuthorizationServer(oAuthServerOptions);
  app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}

ApplicationOAuthProvider
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) {
  //Dummy check here
  if (context.UserName != context.Password) {
    context.SetError("invalid_grant", "The user name or password is incorrect");
    return Task.FromResult<object>(null);
  }

  var claims = new List<Claim> {
    new Claim(ClaimTypes.NameIdentifier, context.UserName),
    new Claim(ClaimTypes.Name, context.UserName)
  };

  var oAuthIdentity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);

  AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());
  context.Validated(ticket);
  return Task.FromResult<object>(null);
}

public override Task TokenEndpoint(OAuthTokenEndpointContext context) {
  foreach (KeyValuePair<string, string> property in context.Properties.Dictionary) {
    context.AdditionalResponseParameters.Add(property.Key, property.Value);
  }

  return Task.FromResult<object>(null);
}

注意:对于那些想知道为什么要存储 token 的人,这是我必须满足的要求。

最佳答案

要在将 token 发送给客户端之前获取 token ,您必须重写TokenEndpointResponse:

public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
{
    return base.TokenEndpointResponse(context);
}
context对象具有AccessToken属性,它将包含 token 的字符串表示形式。

enter image description here
OAuthTokenEndpointResponseContext包含对象字典IDictionary<string, object>中的AdditionalResponseParameters,可让我们查找有关身份的所有声明。

如果我们想获取 token 的到期时间,我们将在字典中找到声明.expires:
context.AdditionalResponseParameters[".expires"]

如果有人有兴趣玩客户端和服务器交互的简单集成,可以使用github repository

关于asp.net-web-api - 存储owin oauth承载 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174319/

相关文章:

php - XERO API 的快速认证技术有哪些?

javascript - SignalR owin 启动没有受到影响。获取 "Object doesn' t 支持属性或方法“javascript 错误

c# - 在查询字符串中使用点 ("."时未设置 CORS header )

asp.net-mvc - 如何将 ControllerContext 传递给属性

angularjs - Angular JS : JSON data is not getting displayed in ng-Grid

c# - 如何使用 HttpContentExtensions.ReadAsAsync<T>()?

javascript - WebApi 捕获连接超时错误

oauth - 使用 Openiddict 的 ASP.NET Core 1.0 OAuth 服务器

command-line - 如何使用 cURL(或任何命令行工具)通过 OAuth 身份验证向 Twitter 发送 HTTP 帖子?

asp.net-mvc - 使用 OWIN 和 app.UseWebApi 在 Web API 中继续使用 cookie 进行身份验证