我们正在构建一个带有角7的Ionic 4应用程序,我们想通过Azure AD v2端点进行身份验证。我们使用马萨角包装器作为msal.js库。我们正在成功地命中端点,身份验证提供者在回调中使用令牌进行响应。
从这里开始我们的问题。msal库不会在移动应用程序上下文中自动处理此令牌,因此我们必须尝试手动处理该令牌。当我们在浏览器中调试应用程序时,msal库会自动保存令牌,并正确登录。
为了重定向到移动应用程序中的页面,我们使用Applinks/德普林克斯 cordova插件,提供被身份验证提供者接受为有效URI的回调URI,并使我们能够重定向到应用程序(以及应用程序中的正确页面)。然而,使用Deeplinks,我们将访问回调,但是MSAL库无法识别回调,因此无法继续它的登录过程来保存令牌并将其状态设置为登录(我们正在利用这个库来保护应用程序中的路由)。
在浏览器中无需Deeplinks调试就可以正常工作。当回调被击中时,我们如何使MSAL库继续它的登录进程?
MSAL配置:
MsalModule.forRoot({
clientID: '******',
authority: "https://login.microsoftonline.com/********", // (Optional) It is the authority URL as described in the configuration section above to support account types. The default authority is https://login.microsoftonline.com/common.
validateAuthority: true,
redirectUri: "https://example.com/callback",
cacheLocation : "localStorage",
postLogoutRedirectUri: "https://example.com/home",
navigateToLoginRequestUrl: false,
popUp: false,
consentScopes: [ "user.read", "api://*************/user_read"],
unprotectedResources: ["https://www.microsoft.com/en-us/"],
correlationId: '1234',
piiLoggingEnabled: true
})
Deeplinks:
this.platform.ready().then(() => {
this.deeplinks.route({
'/home': HomePage,
'/callback': CallbackPage
}).subscribe((match) => {
const idToken = match.$link.fragment;
this.router.navigate(['/callback', {key: idToken}])
},
(nomatch) => {
console.error('Got a deeplink that didn\'t match', nomatch);
});
});
发布于 2020-05-15 15:20:42
我有同样的问题a you,似乎cordova msal插件不再受支持,因此作为替代您所做的工作,请遵循以下步骤(这篇文章中提到的问题不是一个解决方法)。
我最终实现了这个插件:电容OAuth 2客户端插件git:https://github.com/moberwasserlechner/capacitor-oauth2
要安装它,请执行以下操作:
npm i -E @byteowls/capacitor-oauth2
请注意,最小电容版本是2.0.0
插件配置可能会发生变化,我建议您在执行这些步骤之前先阅读它们的初始设置,但是对于天蓝色配置,您应该执行以下操作:
oauth2config
(...) // other configs
android: {
pkceEnabled: true,
responseType: 'code',
redirectUrl: 'com.company.testapp://oauth/redirect',
accessTokenEndpoint: 'https://TENANT.b2clogin.com/TENANT.onmicrosoft.com/B2C_1_policy-signin-signup-web',
handleResultOnNewIntent: true,
handleResultOnActivityResult: true
},
(...) // other configs
strings.xml:
<string name="custom_url_scheme">com.company.testapp://oauth/redirect</string>
AndroidManifest:
<data android:scheme="com.company.testapp" android:host="auth" />
build.gradle:
defaultConfig {
// other stuff
manifestPlaceholders = [
"appAuthRedirectScheme": "com.company.testapp"
]
Azure门户上的:
Include web app / web API: YES
Allow implicit flow: YES
Include native client: YES
Custom Redirect URI: com.company.testapp://oauth/redirect
创建了一个具有此实现的蔚蓝b2c示例的回购(您只需更改com.c....
和与您的实现匹配的信任):https://github.com/loonix/capacitor-oauth2-azure-example
如果您在实现此问题时遇到任何问题,请参考以下问题:https://github.com/moberwasserlechner/capacitor-oauth2/issues/96 https://github.com/moberwasserlechner/capacitor-oauth2/issues/91
https://stackoverflow.com/questions/57586553
复制相似问题