简体   繁体   English

Azure AD B2C 集成在 SwiftUI 应用程序错误“不支持权限验证...”

[英]Azure AD B2C integration in SwiftUI App errors with “Authority validation is not supported…”

I currently try to integrate the Azure AD B2C (Email & Sign In with Apple) into my App.我目前尝试将 Azure AD B2C(通过 Apple 发送电子邮件和登录)集成到我的应用程序中。 When I preview the login page from the Azure Portal everything looks good and works fine.当我从 Azure 门户预览登录页面时,一切看起来都很好并且工作正常。

But when I open the login page from my App I get the following error:但是当我从我的应用程序打开登录页面时,我收到以下错误:

Error Domain=MSALErrorDomain Code=-50000 "(null)" UserInfo={MSALErrorDescriptionKey=Authority validation is not supported for this type of authority, MSALInternalErrorCodeKey=-42008, MSALCorrelationIDKey=2EFF58A4-24F2-4ABC-8D80-BC96EDD26AB7}

Here is my code for the AD B2C part:这是我的 AD B2C 部分代码:

import SwiftUI
import MSAL

class MicrosoftLoginCotroller: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let btn = UIButton(frame: CGRect(x: 20, y: self.view.frame.height - 100, width: self.view.frame.width - 40, height: 52))
        btn.backgroundColor = .green
        btn.setTitle("Lass uns starten!", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        
        self.view.addSubview(btn)
    }
    
    @objc func buttonTapped(_ sender: UIButton) {
        do {
            let authority = try MSALB2CAuthority(url: URL(string: "https://<tenant>.b2clogin.com/tfp/<tenant>.onmicrosoft.com/<policy>")!)
            let pcaConfig = MSALPublicClientApplicationConfig(clientId: "my_correct_client_id_from_the_azure_b2c_ad", redirectUri: nil, authority: authority)
            let application = try MSALPublicClientApplication(configuration: pcaConfig)
            let webViewParameters = MSALWebviewParameters(authPresentationViewController: self)
            let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["user.read"], webviewParameters: webViewParameters)

            application.acquireToken(with: interactiveParameters) { (result, error) in
                guard let result = result else {
                    print("Error MSAL Token")
                    print(error!)
                    return
                }

                if let account = result.account.identifier {
                    print(account)
                    UIApplication.shared.windows.first { $0.isKeyWindow }!.rootViewController = UIHostingController(rootView: SignupProcessView())
                }
            }
        } catch {
            print("Error MSAL")
            print(error)
        }
    }
}

struct MicrosoftLoginView: UIViewControllerRepresentable {
    typealias UIViewControllerType = MicrosoftLoginCotroller
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<MicrosoftLoginView>) -> MicrosoftLoginCotroller {
        return MicrosoftLoginCotroller()
    }
    
    func updateUIViewController(_ uiViewController: MicrosoftLoginCotroller, context: Context) {
        
    }
}

Do you know where my error is?你知道我的错误在哪里吗?

Thank you very much!非常感谢!

After looking through 100+ Github issues I found the problem.在查看了 100+ Github 问题后,我发现了问题。 The SDK doesn't trust the B2C MS domain by default. SDK 默认不信任 B2C MS 域。 So you need to add your authoriser as a trusted domain with:因此,您需要将您的授权人添加为受信任的域:

pcaConfig.knownAuthorities = [authority]

The functional code looks like:功能代码如下所示:

import SwiftUI
import MSAL

class MicrosoftLoginCotroller: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let btn = UIButton(frame: CGRect(x: 20, y: self.view.frame.height - 100, width: self.view.frame.width - 40, height: 52))
        btn.backgroundColor = .green
        btn.setTitle("Lass uns starten!", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        
        self.view.addSubview(btn)
    }
    
    @objc func buttonTapped(_ sender: UIButton) {
        do {
            let authority = try MSALB2CAuthority(url: URL(string: "https://<tenant>.b2clogin.com/tfp/<tenant>.onmicrosoft.com/<policy>")!)
            let pcaConfig = MSALPublicClientApplicationConfig(clientId: "my_correct_client_id_from_the_azure_b2c_ad", redirectUri: nil, authority: authority)
            pcaConfig.knownAuthorities = [authority]
            let application = try MSALPublicClientApplication(configuration: pcaConfig)
            let webViewParameters = MSALWebviewParameters(authPresentationViewController: self)
            let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["user.read"], webviewParameters: webViewParameters)

            application.acquireToken(with: interactiveParameters) { (result, error) in
                guard let result = result else {
                    print("Error MSAL Token")
                    print(error!)
                    return
                }

                if let account = result.account.identifier {
                    print(account)
                    UIApplication.shared.windows.first { $0.isKeyWindow }!.rootViewController = UIHostingController(rootView: SignupProcessView())
                }
            }
        } catch {
            print("Error MSAL")
            print(error)
        }
    }
}

struct MicrosoftLoginView: UIViewControllerRepresentable {
    typealias UIViewControllerType = MicrosoftLoginCotroller
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<MicrosoftLoginView>) -> MicrosoftLoginCotroller {
        return MicrosoftLoginCotroller()
    }
    
    func updateUIViewController(_ uiViewController: MicrosoftLoginCotroller, context: Context) {
        
    }
}

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

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