I currently try to integrate the Azure AD B2C (Email & Sign In with Apple) into my App. When I preview the login page from the Azure Portal everything looks good and works fine.
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:
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. The SDK doesn't trust the B2C MS domain by default. 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) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.