在我的Web中,我希望从请求中的Cookies头获取访问令牌,然后对令牌进行验证。目前,it yServer3.AccessTokenVal环流包用于验证Bearer令牌,并且它只从授权标头中查找令牌。最好我想继续使用相同的承载令牌验证过程,但是从Cookies头获取令牌,这听起来可以用方便的代码吗?谢谢
发布于 2016-05-02 14:12:41
只需实现您自己的TokenProvider
并将其提供给AccessTokenValidationMiddleware
public class MyCustomTokenProvider : IOAuthBearerAuthenticationProvider
{
public Task RequestToken(OAuthRequestTokenContext context)
{
if (context.Token == null)
{
//try get from cookie
var tokenCookie = context.Request.Cookies["myCookieName"];
if (tokenCookie != null)
{
context.Token = tokenCookie;
}
}
return Task.FromResult(0);
}
public Task ValidateIdentity(OAuthValidateIdentityContext context)
{
throw new NotImplementedException();
}
public Task ApplyChallenge(OAuthChallengeContext context)
{
throw new NotImplementedException();
}
}
在你的Startup.cs
里
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://myhost",
RequiredScopes = new[] { "my-scope" },
TokenProvider = new MyCustomTokenProvider()
});
https://stackoverflow.com/questions/36983824
复制相似问题