首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JwtBearerAuthentication与ASP.Net 5和ASP.NET MVC 6

JwtBearerAuthentication与ASP.Net 5和ASP.NET MVC 6
EN

Stack Overflow用户
提问于 2015-12-17 21:47:26
回答 1查看 4.3K关注 0票数 2

我试图让JwtBearerAuthentication在ASP.Net 5,MVC6 WebApi应用程序中工作。我在令牌端点生成了Jwt令牌,但是当我尝试使用该令牌访问所有WebApi端点时,我得到了以下错误:

System.InvalidOperationException: IDX10803:无法从:IDX10803获得配置

这是我的Startup.cs中的代码,有人能告诉我我做错了什么吗?

代码语言:javascript
运行
复制
public Startup(IHostingEnvironment env)
{
    const string _TokenIssuer = "http://localhost:5000/";
    const string _TokenAudience = "http://localhost:5000/";

public void ConfigureServices(IServiceCollection services)
    {
    ...
        RsaSecurityKey _signingKey = null;
        SigningCredentials signingCredentials = null;
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
        {
            signingKey = new RsaSecurityKey(rsa.ExportParameters(true));
            signingCredentials = new SigningCredentials(_SigningKey, SecurityAlgorithms.RsaSha256Signature);
        }
        services.AddInstance<SigningCredentials>(signingCredentials);  // tobe used in JwtTokenHandler in the Token Controller

    var jwtOptions = new JwtBearerOptions();
        jwtOptions.AutomaticAuthenticate = true;
        jwtOptions.TokenValidationParameters.IssuerSigningKey = signingKey;
        jwtOptions.TokenValidationParameters.ValidAudience = _TokenAudience;
        jwtOptions.TokenValidationParameters.ValidIssuer = _TokenIssuer;
        services.AddInstance<JwtBearerOptions>(jwtOptions);   //tobe used in the Token Controller

        services.AddAuthorization(auth =>
        {
            auth.AddPolicy(JwtBearerDefaults.AuthenticationScheme, new AuthorizationPolicyBuilder()
                .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                .RequireAuthenticatedUser().Build());
        });

        services.AddMvc();
        services.AddAuthentication();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ...
        //Add Bearer Authentication Middleware, make sure this is before UseIdentity line
        app.UseJwtBearerAuthentication(options =>
        {
            options.AutomaticAuthenticate = true;
            options.Audience = _TokenAudience;
            options.Authority = _TokenIssuer;
            options.RequireHttpsMetadata = false;
        });

        app.UseIdentity();
        app.UseMvc();
    }
}

我用Kestrel来主持,我的服务网址是http://localhost:5000/XXX

编辑:--在验证令牌时,JwtBearerAuthentication仍然有问题。我切换到使用OpenIdConnectServer来发出令牌。那部分没问题。但是,当尝试使用JwtBearerAuthentication验证令牌时,除非我设置了options.TokenValidationParameters.ValidateAudience = false,否则总是得到SecurityTokenInvalidAudienceException: IDX10208: Unable to validate audience. validationParameters.ValidAudience is null or whitespace and validationParameters.ValidAudiences is null

当我将其设置为false时,我可以按下我的WebApi代码,但是,当我检查控制器中的User对象时,没有设置User.Identity.NameUser.Identity.Authenticationtype="Authentication.Federation"User.Identity.IsAuthenticated = true.我可以看到标识下的声明列表,但并不是我添加到令牌中的所有声明都在那里。这与我在.Net 4.5中使用UseOAuthBearerAuthentication时看到的情况确实不同。

我把我的测试项目放在了GitHub https://github.com/Sally-Xu/Net5Start。你能看出是怎么回事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-19 15:18:51

您所看到的错误是由您将Authority设置为非空值造成的。只有当令牌由OpenID连接服务器(如AspNet.Security.OpenIdConnect.Server )发出时,才应使用此属性。由于您使用的是自定义令牌控制器,所以我猜它不是真正的OAuth2/OpenID服务器,这解释了为什么您会看到这个错误。

停止设置此属性,它应该可以工作。请注意,您必须在IssuerSigningKey调用UseJwtBearerAuthentication中直接设置ConfigureServices

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34344860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档