2016-11-28 40 views
0

我遇到與this相同的問題,但他們的解決方案對我無效。401身份驗證使用Xamarin和IdentityModel對身份服務器3保護的WebAPI 3

我稱爲的WebAPI方法(.NET 4.5.2)和項目具有IdentityModel 1.13.1的引用,它使用IdentityServer 3在啓動類下面的代碼保護 -

 JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); 

     app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "https://localhost:44305/core/", 
      RequiredScopes = new[] { "read", "write" }, 

      // client credentials for the introspection endpoint 
      ClientId = "clientcredentials.client", 
      ClientSecret = "secret" 
     }); 

在IdentityServer啓動客戶端的配置包括客戶端的定義 -

new Client 
      { 
       ClientName = "Mobile Api Client", 
       Enabled = true, 
       ClientId = "clientcredentials.client", 
       Flow = Flows.ClientCredentials, 

       ClientSecrets = new List<Secret> 
        { 
         new Secret("secret".Sha256()), 
         new Secret 
         { 
          Value = "[valid thumbprint]", 
          Type = "X509Thumbprint", 
          Description = "Client Certificate" 
         }, 
        }, 

       AllowedScopes = new List<string> 
        { 
         "read", 
         "write" 
        }, 

       Claims = new List<Claim> 
        { 
         new Claim("location", "datacenter") 
        } 
      } 

而在Xamarin客戶端(也使用IdentityModel 1.13.1)...

  var token = IdentityServerClient.RequestClientToken(); // this returns a valid bearer token 
      TokenResultLabel.Text = token.Raw; 

      HttpClient apiClient = new HttpClient(); 

      apiClient.SetBearerToken(token.AccessToken); 
      var result = await apiClient.GetStringAsync("[valid api URL]"); 
      ApiResultLabel.Text = result; 

我在IdentityServer與IdentityModel 2.0(兼容最新版本),1.13.1(中引用的問題中提到的版本,1.9.2(版本嘗試了3個樣本)

任何幫助將不勝感激

+0

您需要打開您的API中的日誌記錄 - https://identityserver.github.io/Documentation/docsv2/consuming/diagnostics.html – leastprivilege

+0

感謝您的建議。我已經嘗試過,但是沒有生成日誌文件。最後,我放棄了並重新創建了WebAPI項目,但這次我使用了空白Web項目模板,並添加了WebAPI參考,之前我使用了帶有WebAPI參考的MVC模板。我不知道有什麼區別,但它的工作。再次感謝 –

回答

0

儘管配置允許客戶端同時請求readwrite作用域,您是否明確指定您希望獲取包含這兩個作用域的訪問令牌的時間?這應該發生在您的IdentityServerClient.RequestClientToken方法中。

允許客戶端請求作用域並不意味着這些作用域將自動包含在由IdentityServer返回的訪問令牌中。