ASP.NET JSON Web令牌“401 Unauthorized”
|
我正在使用分离的资源和身份验证服务器.
请求具有授权标头: Authorization: Bearer TOKEN_HERE 响应总是“401 Unauthorized”: {
"message": "Authorization has been denied for this request."
}
这是我的资源服务器的Startup.cs using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Jwt;
using Newtonsoft.Json.Serialization;
using Owin;
using System.Web.Http;
using Test.Database;
using Test.Infrastructure;
using Microsoft.WindowsAzure.ServiceRuntime;
[assembly: OwinStartup(typeof(Test.API.Startup))]
namespace Custodesk.API
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() =>
ApplicationDbContext.Create(RoleEnvironment.GetConfigurationSettingValue("SqlConnectionString")));
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
GlobalConfiguration.Configuration.SuppressDefaultHostAuthentication();
ConfigureOAuthTokenConsumption(app);
GlobalConfiguration.Configure(config =>
{
//global filters
config.Filters.Add(new AuthorizeAttribute());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",routeTemplate: "{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(GlobalConfiguration.Configuration);
}
private void ConfigureOAuthTokenConsumption(IAppBuilder app)
{
var issuer = "http://localhost";
var audience = "Universal_application";
var secret = Helper.GetHash("helper_class_to_get_the_same_hash_as_authentication_server");
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,AllowedAudiences = new[] { audience },IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer,secret)
}
});
}
}
}
以下是令牌解密的示例: {
"typ": "JWT","alg": "HS256"
}
{
"nameid": "b22a825e-60ce-45ed-b2cb-b2ee46a47936","unique_name": "begunini","role": [
"Owner","Admin","ManagerViewer"
],"iss": "http://localhost","aud": "Universal_application","exp": 1454876502,"nbf": 1454876202
}
我已经检查了秘密,双方都是相同的(身份验证和资源服务器). 我猜配置顺序有一些问题,但没有任何帮助. 有任何想法吗 ? 解决方法TL; DR:您是否尝试删除GlobalConfiguration.Configuration.SuppressDefaultHostAuthentication()?使用此方法时,Web API将删除由Web主机(在您的情况下由JWT承载中间件)注册的主机或中间件创建并添加到OWIN上下文的用户主体. 此方法旨在与HostAuthenticationFilter或HostAuthenticationAttribute一起使用,该方法直接调用与指定的身份验证类型对应的身份验证中间件,并在OWIN上下文中保留生成的用户主体. 由于您在没有HostAuthenticationAttribute的情况下使用SuppressDefaultHostAuthentication,因此Web API始终会看到未经身份验证的请求,这就是AuthorizeAttribute拒绝它们的原因. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 处理单数和多个控制器/路由
- asp.net – 用户控件的属性在回发后失去价值
- asp.net – 对于每个循环都有控件并在MVC 4中提交表单(Html
- asp.net-mvc-3 – 如何在ASP.NET MVC3控制器中访问autofac容
- asp.net – 什么是Container.DataItem?
- asp.net-mvc – 根据浏览器接受语言自动设置uiCulture
- log4net在ASP.NET中的唯一请求ID
- asp.net – 是否可以将.ASPXAUTH用于我自己的日志系统?
- ASP.NET Core 1 RC2 Web应用入门点
- asp.net-mvc – 使用JSON结果填充下拉列表 – 使用MVC3,JQu
- asp.net – 处理程序执行子请求时出错’System.W
- asp.net-mvc – 可以通过RedirectToAction传递模
- asp.net-mvc-2 – 无法加载文件或程序集’System
- asp.net-mvc – 测试控制器使用User.Identity.Na
- asp.net-mvc – MVC DropDownListFor和StringLen
- asp.net中.aspx页面中各种符号的含义
- asp.net-mvc-4 – 最小和最大字符串长度的单独错
- .net – 在控制器中查看列表数据
- asp.net-mvc-4 – ASP.Net MVC 4和WebSecurity –
- asp.net – 经过身份验证的服务不支持跨域javasc
