IdentityServer3 .Net Web API。获取错误 - 授权已拒绝此请求

问题描述:

我正在尝试使用identityserver3为我的.net webapi设置身份验证。IdentityServer3 .Net Web API。获取错误 - 授权已拒绝此请求

这是我在认证服务器项目

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([email protected]"{AppDomain.CurrentDomain.BaseDirectory}\bin\my_selfsigned_cert.pfx", ConfigurationManager.AppSettings["certificatekey"]), 
      RequireSsl = false, 
      Factory = factory 
     }); 
    } 

的Owin.Startup代码和以下是我的网络API代码owin启动

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

     app.UseWebApi(GlobalConfiguration.Configuration); 
    } 
} 

我的授权服务器时,似乎工作我尝试登录身份服务器登录页面。我也可以通过发布到/connect/token 来获取授权令牌。但是,当我使用这样收到的不记名令牌调用我的webapi方法时,它总是失败,出现错误“{”Message“:”此请求的授权已被拒绝。 }

API - ?

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

有人可以请建议我缺少什么

+0

可能是错误的,但是您是否需要指定应用程序的范围? – uk2k05

+1

[https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html](https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html)似乎有一些体面的屏幕抓取关于如何设置Startup.cs并创建正确的作用域 – uk2k05

+0

将scope-openid传递给auth服务器,同时对/ connect/token发送邮件以获取访问令牌。此外,可用范围列表作为内存集合提供给auth服务器(提供代码)。屏幕截图解释了不同的流量/授权,它使用登录页面。在我的情况下,我正在保护我的webapi,并且没有涉及ui。 – Anoop

Microsoft.Owin.Host.SystemWeb

按照here中的建议将此nuget安装到我的web api项目中,并开始工作!