简体   繁体   English

IdentityServer3 .Net Web API。 收到错误-该请求的授权被拒绝

[英]IdentityServer3 .Net Web API. Getting error - Authorization has been denied for this request

I am trying to setup authentication for my .net webapi using identityserver3. 我正在尝试使用identityserver3为.net webapi设置身份验证。

This is my code in Owin.Startup of the Authentication server project 这是我在Owin.Authentication服务器项目启动中的代码

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // hardcoded list of clients, scopes and users

        var factory = new IdentityServerServiceFactory()
            .UseInMemoryClients(clients)
            .UseInMemoryScopes(scopes)
            .UseInMemoryUsers(users);

        app.UseIdentityServer(new IdentityServerOptions
        {
            SigningCertificate = new X509Certificate2($@"{AppDomain.CurrentDomain.BaseDirectory}\bin\my_selfsigned_cert.pfx", ConfigurationManager.AppSettings["certificatekey"]),
            RequireSsl = false,
            Factory = factory
        });
    }

And the following is the code in my web api owin startup 以下是我的Web API owin启动中的代码

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = "http://localhost:45230"
        });

        app.UseWebApi(GlobalConfiguration.Configuration);
    }
}

My authorization server seems to work when I try to login in the identity servers login page. 当我尝试在身份服务器登录页面中登录时,授权服务器似乎可以正常工作。 I am also able to retrieve authorization token by posting to /connect/token However when I use the bearer token thus received to call my webapi method below, it's always failing with error "{"Message": "Authorization has been denied for this request."} 我还可以通过发布到/connect/token来检索授权令牌。但是,当我使用收到的承载令牌来调用下面的webapi方法时,它总是失败,并显示错误“ {”消息”:“此请求的授权被拒绝。“}

Api - api-

[HttpGet]
    [Authorize]
    public IEnumerable<Customer> Get()
    {
        var customerRepository = new CustomerRepository();
        return customerRepository.GetCustomers();
    }

Can somebody please suggest what i am missing ? 有人可以建议我缺少什么吗?

Microsoft.Owin.Host.SystemWeb Microsoft.Owin.Host.SystemWeb

Installed this nuget to my web api project as suggested in here and it started working !! 按照此处的建议将此nuget安装到我的Web api项目中,它开始工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此请求的授权已被拒绝 - 新的 Web API 项目 - Authorization has been denied for this request - New Web API Project 此请求的授权已被拒绝 - 桌面到 ASP.NET Web API - Authorization has been denied for this request - Desktop to ASP.NET Web API Azure AD 带有用于 Web API 的不记名令牌身份验证的 AD 无法正常工作,抛出错误,因为“此请求的授权已被拒绝”。 - Azure AD with Bearer token authentication for Web API not working throwing error as “Authorization has been denied for this request.” 此请求被拒绝获得授权 - Getting Authorization has been denied for this request 使用API​​密钥验证创建ASP.NET Web API时,授权被拒绝 - Authorization has been denied when creating ASP.NET Web API with API Key validation 始终获得“此请求已被拒绝授权。”消息 - Always getting the “Authorization has been denied for this request.” message 此请求的授权已被拒绝:JWT承载 - Authorization has been denied for this request : JWT Bearer 此请求的C#授权被拒绝 - C# Authorization has been denied for this request 此请求的授权已被拒绝。 邮差 - Authorization has been denied for this request. Postman OAuth令牌授权(请求已被拒绝) - OAuth token authorization (request has been denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM