2017-02-13 86 views
1

我们遇到了令刷新令牌发挥作用的问题。最初,用户使用ADAL的Web视图登录并获取令牌。该令牌用于在Web API过期之前调用它。如我们所期望的那样,如果没有获得Web提示的新令牌,服务器上会记录一个错误,并且用户会再次显示登录Web提示。使用ADFS 3.0,ADAL,Web API和Xamarin刷新令牌

从我们已阅读的内容中,您应该在每次调用时使用AcquireTokenAsync并让ADAL处理令牌/刷新令牌。

这是ADAL尝试使用刷新令牌获取新令牌时在服务器上发生的错误。我们试图寻找那个错误,但没有发现太多。

在OAuth令牌请求期间遇到错误。

附加数据

异常详细信息: Microsoft.IdentityServer.Web.Protocols.OAuth.Exceptions.OAuthInvalidScopeException: MSIS9330:接收OAuth访问令牌请求是无效的。在请求中接收到' '范围'参数,并且AD FS不支持任何范围 。收到的范围:'openid'。在 Microsoft.IdentityServer.Web.Protocols.OAuth.OAuthToken.OAuthRefreshTokenRequestContext.Validate()

难道我们失去了一些东西?有没有办法设置范围,或者这只是不适用于我们正在使用的当前版本? ADAL是将范围发布到ADFS服务器的人。

我们不使用Azure AD!

从视图控制器在iOS应用呼叫:

PlatformParameters p = new PlatformParameters(this); 

AuthenticationContext authContext = new AuthenticationContext("https://adfs.domain.com/adfs", false); 

AuthenticationResult _authResult = await authContext.AcquireTokenAsync("https://webapi.domain.com", "E1CF1107-FF90-4228-93BF-26052DD2C714", “http://anarbitraryreturnuri/”, p); 

Startup.Auth.cs在网页API:

public void ConfigureAuth(IAppBuilder app) 
     { 
      app.UseActiveDirectoryFederationServicesBearerAuthentication(
       new ActiveDirectoryFederationServicesBearerAuthenticationOptions 
       { 
        MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"], 
        TokenValidationParameters = new TokenValidationParameters() 
        { 
         ValidAudience = ConfigurationManager.AppSettings["ida:Audience"], 
        }, 
       } 
     } 

这里是我们的作品:

  • 带有ADFS 3.0的Windows Server 2012 R2(内部部署)
  • SsoLifetime = 60
  • TokenLifetime(依赖方)= 10
  • ADAL 3.13.8
  • .NET的Web API
  • Xamarin iOS应用

下面是一些职位,我们用得到这个工作:

http://www.cloudidentity.com/blog/2013/10/25/securing-a-web-api-with-adfs-on-ws2012-r2-got-even-easier/

http://www.cloudidentity.com/blog/2015/08/13/adal-3-didnt-return-refresh-tokens-for-5-months-and-nobody-noticed/

回答

1

该问题出现在ADAL源代码中。服务器日志中的错误非常具体:

在请求中收到'scope'参数,并且AD FS不支持任何范围。收到的范围:'openid'。 ADAL库在尝试获取刷新令牌时会发送一个scope参数。 ADFS 3.0不支持openid范围,因此失败。

从GitHub下载ADAL码 - https://github.com/AzureAD/azure-activedirectory-library-for-dotnet

打开AcquireTokenHandlerBase.cs位于:

protected async Task<AuthenticationResultEx> SendTokenRequestByRefreshTokenAsync(string refreshToken) 
    { 
     var requestParameters = new DictionaryRequestParameters(this.Resource, this.ClientKey); 
     requestParameters[OAuthParameter.GrantType] = OAuthGrantType.RefreshToken; 
     requestParameters[OAuthParameter.RefreshToken] = refreshToken; 
     //requestParameters[OAuthParameter.Scope] = OAuthValue.ScopeOpenId; **This line causes refresh to fail** 

     AuthenticationResultEx result = await this.SendHttpMessageAsync(requestParameters).ConfigureAwait(false); 

     if (result.RefreshToken == null) 
     { 
      result.RefreshToken = refreshToken; 
      PlatformPlugin.Logger.Verbose(this.CallState, 
       "Refresh token was missing from the token refresh response, so the refresh token in the request is returned instead"); 
     } 

     return result; 
    } 

干净:

enter image description here

从SendTokenRequestByRefreshTokenAsync通话中移除范围重建项目。将nuget引用替换为新的DLL。确保在Xamarin iOS项目中包含平台DLL。

enter image description here

现在,当ADAL试图获取刷新令牌它应该会成功。

enter image description here