WebApi - 在 context.Validated() 之后获取 OAuth 不记名令牌

WebApi - Getting the OAuth bearer token after context.Validated()

我希望这里很简单 :) 我有一个 API 的 OAuth 令牌端点,并且一切正常,但是,我想将令牌存储在数据库中。

在……

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)

我有以下代码......

var identity = new ClaimsIdentity(context.Options.AuthenticationType);

identity.AddClaim(new Claim("Username", context.UserName));
identity.AddClaim(new Claim("IpAddress", context.OwinContext.Request.RemoteIpAddress));
identity.AddClaim(new Claim("TokenReceivedDate", DateTime.Now.ToString()));

//  Now store the token in the database.

context.Validated(identity);

我可以拿到此时要发行的不记名令牌吗?理想情况下,我想将其存储在数据库中以供遗留应用程序使用。挖掘所有对象 类似于 的对象(可能)持有令牌似乎是 NULL

已解决,覆盖'TokenEndPointReponse'即可轻松挖出生成的Token。

public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
{
    string thisIsTheToken = context.AccessToken;
    return base.TokenEndpointResponse(context);
}