2017-06-19 81 views
0

我正在嘗試使用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

回答

0

Microsoft.Owin.Host.SystemWeb

按照here中的建議將此nuget安裝到我的web api項目中,並開始工作!

相關問題