我已经配置我的Web API 2项目通过OAuth合作使用OWIN获得授权已被拒绝了这个请求
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new MyAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
MyAuthorizationServerProvider的实施:
public class MyAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var repo = new AuthRepository();
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var user = repo.ValidateUserCredentials(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
}
我能得到一个令牌.. 然后我把它发送到一个控制器装饰与[Authorized]
仍然越来越
{
"message": "Authorization has been denied for this request."
}
与令牌这里的请求头:
Content-Type:application/json
Authorization:Bearer m6ukGHOmp_MgM6rsb70n8Rd_BlBEXbgyPNr5ZNzGzkm__7zdntVPYAMAhFPXVdw1S6-ohv960c6mzMMwkY0G_AL-2uIq2aOjkoojxDPKuITuml8sgC4ng0KC2AqUFrBBEn0eCsSAnc3UK_jyKSB59Ao_eL9JPoJHoJHP-xBs42KGAGl4iKdYtzdfg2zY0Ucif_IpPH3Gbvs6iyLVMf0jlKgj4EOBtrDHanikeqkWc0W2pWjIIac8cyUrOtZCj7h3
我在做什么错在这里?
另外..奇迹..存储的令牌在哪里? Web API如何验证收到的令牌是否附加到有问题的用户?
我是否缺少一些令牌存储类的自定义实现?如果是的话,我在哪里可以找到关于如何实现自定义令牌存储的信息?