OAuth 令牌授权(请求已被拒绝)
OAuth token authorization (request has been denied)
我在同一解决方案 运行 的不同 IIS 端口上有一个 WebApi 2 和一个 MVC Web 项目。使用 jQuery AJAX 收到我的 Oauth 令牌后,我在尝试调用授权的控制器方法时仍然收到 401 未授权错误消息。
启动:
public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
ConfigureOAuthTokenGeneration(app);
ConfigureOAuthTokenConsumption(app);
ConfigureWebApi(httpConfig);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(httpConfig);
}
CustomOAuthProvider:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<UserManager>();
User user = await userManager.FindAsync(context.UserName, context.Password);
// checks with context.SetError() results.
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));
var ticket = new AuthenticationTicket(oAuthIdentity, null);
context.Validated(ticket);
}
我认为我已经尝试过 I get "Authorization has been denied for this request." error message when using OWIN oAuth middleware (with separate Auth and Resource Server):
- 正在将所有 Owin 包更新到最新版本(Web 项目不使用任何 Owin 功能,因此未在此处安装)。
- Api 和 web 是不同的项目,但在同一台机器上,因此机器密钥相同。
- OAuth Token 配置先于 Startup.cs 中的 WebApi 配置。
- 提出声明:oAuthIdentity 由角色和管理员声明组成(http://schemas.microsoft.com/ws/2008/06/identity/claims/role:用户)
其他一切都按预期工作(web api、cors、令牌生成...),我做错了什么? (涉及到很多代码,所以如果我需要从我的项目中放置另一段代码,请告诉我。
编辑:
Ajax 调用(jumuro 的解决方案):
var token = sessionStorage.getItem(tokenKey); // Same as the generated login token
$.ajax({
type: 'POST',
// Don't forget the 'Bearer '!
beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token) },
url: 'http://localhost:81/api/auth/test', // Authorized method
contentType: 'application/json; charset=utf-8'
}).done(function (data) {
//
});
您必须在 ajax 调用中包含 Authorization
header 和不记名令牌。请以 reponse 为例。
希望对你有帮助。
我在同一解决方案 运行 的不同 IIS 端口上有一个 WebApi 2 和一个 MVC Web 项目。使用 jQuery AJAX 收到我的 Oauth 令牌后,我在尝试调用授权的控制器方法时仍然收到 401 未授权错误消息。
启动:
public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
ConfigureOAuthTokenGeneration(app);
ConfigureOAuthTokenConsumption(app);
ConfigureWebApi(httpConfig);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(httpConfig);
}
CustomOAuthProvider:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<UserManager>();
User user = await userManager.FindAsync(context.UserName, context.Password);
// checks with context.SetError() results.
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));
var ticket = new AuthenticationTicket(oAuthIdentity, null);
context.Validated(ticket);
}
我认为我已经尝试过 I get "Authorization has been denied for this request." error message when using OWIN oAuth middleware (with separate Auth and Resource Server):
- 正在将所有 Owin 包更新到最新版本(Web 项目不使用任何 Owin 功能,因此未在此处安装)。
- Api 和 web 是不同的项目,但在同一台机器上,因此机器密钥相同。
- OAuth Token 配置先于 Startup.cs 中的 WebApi 配置。
- 提出声明:oAuthIdentity 由角色和管理员声明组成(http://schemas.microsoft.com/ws/2008/06/identity/claims/role:用户)
其他一切都按预期工作(web api、cors、令牌生成...),我做错了什么? (涉及到很多代码,所以如果我需要从我的项目中放置另一段代码,请告诉我。
编辑:
Ajax 调用(jumuro 的解决方案):
var token = sessionStorage.getItem(tokenKey); // Same as the generated login token
$.ajax({
type: 'POST',
// Don't forget the 'Bearer '!
beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token) },
url: 'http://localhost:81/api/auth/test', // Authorized method
contentType: 'application/json; charset=utf-8'
}).done(function (data) {
//
});
您必须在 ajax 调用中包含 Authorization
header 和不记名令牌。请以 reponse 为例。
希望对你有帮助。