简体   繁体   English

Azure B2C刷新令牌功能在iOS Swift示例应用程序中不起作用

[英]Azure B2C Refresh Token Functionality Not Working In iOS Swift Sample App

I started working with Azure-Samples/active-directory-b2c-ios-swift-native-msal app to demo the functionality of our Azure B2C tenant. 我开始使用Azure-Samples / active-directory-b2c-ios-swift-native-msal应用程序来演示Azure B2C租户的功能。 I have gotten everything working outside of the refresh token functionality. 我已经在刷新令牌功能之外进行了所有工作。 I have added the "offline_access" scope to ensure that the refresh token is provided. 我添加了“ offline_access”作用域,以确保提供了刷新令牌。

The first error that I got: 我得到的第一个错误:

User Nil error 用户无错误

let application = try MSALPublicClientApplication.init(clientId: kClientID, authority: kAuthority)
let thisUser = try self.getUserByPolicy(withUsers: application.users(), forPolicy: kSignupOrSigninPolicy)

application.acquireTokenSilent(forScopes: kScopes, user: thisUser) { (result, error) in
if error == nil {
self.accessToken = (result?.accessToken)!
self.loggingText.text = "Refreshing token silently"
self.loggingText.text = "Refreshed Access token is \(self.accessToken)"

            } 

So I tried to store the MSAL user from the initial authorization and passed it into the AcquireTokenSilent method. 因此,我尝试从初始授权中存储MSAL用户,并将其传递到AcquireTokenSilent方法中。

I get this error: 我收到此错误:

Failed to find any access token error 找不到任何访问令牌错误

let application = try MSALPublicClientApplication.init(clientId: kClientID, authority: kAuthority)
let thisUser = userFromAuth

    application.acquireTokenSilent(forScopes: kScopes, user: thisUser) { (result, error) in
            if error == nil {
                self.accessToken = (result?.accessToken)!
                self.loggingText.text = "Refreshing token silently"
                self.loggingText.text = "Refreshed Access token is \(self.accessToken)"

            }

Finally, I tried to add the authority/policy used in the SignUp/SignIn (initial auth call) into the AcquireTokenSilent and I get this error: 最后,我尝试将SignUp / SignIn(初始身份验证调用)中使用的权限/策略添加到AcquireTokenSilent中,并且出现以下错误:

I get: "No tokens matching this arguments found in the cache." 我得到:“在缓存中找不到与该参数匹配的令牌。” (Wouldn't let me post a 3rd link) (不允许我发布第三个链接)

let application = try MSALPublicClientApplication.init(clientId: kClientID, authority: kAuthority)
let thisUser = userFromAuth

    application.acquireTokenSilent(forScopes: kScopes, user: thisUser, authority: kAuthority) { (result, error) in
            if error == nil {
                self.accessToken = (result?.accessToken)!
                self.loggingText.text = "Refreshing token silently"
                self.loggingText.text = "Refreshed Access token is \(self.accessToken)"

            }     

I have tested the refresh token functionality in the Android sample app and I am able to refresh the token successfully so I don't think the issue is anywhere in our B2C. 我已经在Android示例应用程序中测试了刷新令牌功能,并且能够成功刷新令牌,因此我认为问题不在我们的B2C中。 I also read that the MSAL library handles the refresh differently than the AppAuth library used in the Android and Obj-C examples so I am not sure if there is something I am missing. 我还了解到,MSAL库处理刷新的方式与Android和Obj-C示例中使用的AppAuth库不同,因此我不确定是否缺少某些内容。

Any insight into what could be wrong would be great! 对可能出问题的任何见解都很棒!

Got it worked out. 知道了。 The useridentifier is returned with the policy name in all lowercase (ex: 56d56ec5-96a9-4c23-9717-4ae5d86f967c-b2c_1_policy) so it will fail to find the user (and the token) if you're policy has any capital letters. 返回的useridentifier带有所有小写的策略名称(例如:56d56ec5-96a9-4c23-9717-4ae5d86f967c-b2c_1_policy),因此,如果您的策略有任何大写字母,它将找不到用户(和令牌)。

I fixed it by adding .lowercased() to the end of the forPolicy string in the getUserByPolicy method: 我通过在getUserByPolicy方法的forPolicy字符串的末尾添加.lowercased()来解决此问题:

for user in withUsers {
    if (user.userIdentifier().components(separatedBy: ".")[0].hasSuffix(forPolicy.lowercased())) {
        return user
  }    
}

Also had to be sure to be on the latest version of Xcode (8.3.3). 还必须确保使用最新版本的Xcode(8.3.3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM