【发布时间】:2017-04-09 13:47:02
【问题描述】:
在我的基于 Web Api 2.2 OWIN 的应用程序中,我遇到了一种情况,我需要手动解码承载令牌,但我不知道该怎么做。 这是我的startup.cs
public class Startup
{
public static OAuthAuthorizationServerOptions OAuthServerOptions { get; private set; }
public static UnityContainer IoC;
public void Configuration(IAppBuilder app)
{
//Set Auth configuration
ConfigureOAuth(app);
....and other stuff
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new AuthProvider(IoC.Resolve<IUserService>(), IoC.Resolve<IAppSettings>())
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
在我的控制器中,我将不记名令牌作为参数发送
[RoutePrefix("api/EP")]
public class EPController : MasterController
{
[HttpGet]
[AllowAnonymous]
[Route("DC")]
public async Task<HttpResponseMessage> GetDC(string token)
{
//Get the claim identity from the token here
//Startup.OAuthServerOptions...
//..and other stuff
}
}
如何手动解码并从作为参数传递的令牌中获取声明?
注意:我知道我可以在标头中发送令牌并使用 [Authorize] 和 (ClaimsIdentity)User.Identity 等,但问题是当令牌中没有出现时如何读取令牌标题。
【问题讨论】:
标签: c# asp.net-web-api oauth-2.0 owin bearer-token