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

时间:2017-08-30 15:22:53

标签: ios swift azure-ad-b2c msal

我开始使用Azure-Samples / active-directory-b2c-ios-swift-native-msal应用来演示Azure B2C租户的功能。我已经完成了刷新令牌功能之外的所有工作。我添加了" offline_access"范围,以确保提供刷新令牌。

我收到的第一个错误:

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)"

            } 

因此,我尝试将MSAL用户存储在初始授权中,并将其传递给AcquireTokenSilent方法。

我收到此错误:

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)"

            }

最后,我尝试将SignUp / SignIn(初始auth调用)中使用的权限/策略添加到AcquireTokenSilent中,我收到此错误:

我得到:"没有匹配缓存中找到的这些参数的令牌。" (不允许我发布第3个链接)

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)"

            }     

我已在Android示例应用中测试了刷新令牌功能,并且我能够成功刷新令牌,因此我不认为问题出现在我们的B2C中。我还读到MSAL库处理刷新的方式与Android和Obj-C示例中使用的AppAuth库的处理方式不同,因此我不确定是否存在我遗漏的内容。

任何可能出错的见解都会很棒!

1 个答案:

答案 0 :(得分:0)

搞定了。返回的useridentifier的策略名称全部为小写(例如:56d56ec5-96a9-4c23-9717-4ae5d86f967c-b2c_1_policy),因此如果您的策略有任何大写字母,它将无法找到用户(和令牌)

我通过在getUserByPolicy方法中将.lowercased()添加到forPolicy字符串的末尾来修复它:

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

还必须确保使用最新版本的Xcode(8.3.3)。