2016-11-28 42 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返回的访问令牌中。