如何在Ionic项目中使用Ionic Native - MS Adal?

9

我是Ionic的新手,想要使用Azure进行用户认证。因此,在我的项目中使用了MS ADAL Ionic Native。我在网上找不到任何合适的例子。以下是我的app.component.ts:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic- 
native/ms-adal';

import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;

  constructor(private msAdal: MSAdal,platform: Platform, statusBar: 
              StatusBar, splashScreen: SplashScreen) {
       platform.ready().then(() => {

           let authContext: AuthenticationContext= 
this.msAdal.createAuthenticationContext('https://login.windows.net/common');

  authContext.acquireTokenAsync('https://graph.windows.net', '[My-appID]', 'http://localhost:8000')
    .then((authResponse: AuthenticationResult) => {
       console.log('Token is' , authResponse.accessToken);
       console.log('Token will expire on', authResponse.expiresOn);
    })
    .catch((e: any) => 
    alert(e));
    statusBar.styleDefault();
    splashScreen.hide();
     });
   }
 }

我收到了以下错误信息。
TypeError: AuthenticationContext.acquireTokenAsync的参数"userId"类型错误:期望为字符串,但实际传入的是函数。

我也有同样的问题。所以我设置了边界,这样有人可以帮助你。 - Karnan Muthukumar
请问您能否在 https://stackblitz.com/ 上创建一个演示? - Paresh Gami
@KarnanMuthukumar 这不应该发生。你可以尝试通过将userId(可选)参数值设置为null来解决吗?authContext.acquireTokenAsync('https://graph.windows.net', '[My-appID]', 'http://localhost:8000', null) - Shantanu
1
好的,让我们再试一次。谢谢您的回复。 - Karnan Muthukumar
我已经更新了你的代码。我遇到了这种类型的错误。AuthenticationContext.acquireTokenAsync的参数“extraQueryParameters”的类型错误:期望字符串,但得到了函数。 - Karnan Muthukumar
这个错误是在catch中返回的还是在构建时出现的? - Josh Stevens
1个回答

3

我已经想出了一个解决办法,我会在下面发布我的答案。如果有人遇到同样的问题,他们可以尝试这个方法。

在acquireTokenAsync函数中发送空字符串作为userId和extraQueryParameters参数,这对我解决了问题。

let authContext: AuthenticationContext=
this.msAdal.createAuthenticationContext('https://login.windows.net/common');
authContext.acquireTokenAsync("https://graph.windows.net", "[My-appID]",
"http://localhost:8000","","")
.then((authResponse: AuthenticationResult) => {
console.log('Token is' , authResponse.accessToken);
console.log('Token will expire on', authResponse.expiresOn);
})
.catch((e: any) =>
alert(e));

我正在寻找相同的内容,但您能解释一下我传递了哪个URL以及我的应用程序ID是什么吗? - Khurshid Ansari
在createauthencationcontext和acuiredtokenasync中,url传递是什么意思? - Khurshid Ansari
是的。但我想在Ionic 3中使用Windows身份验证。 - Khurshid Ansari
嗨,@Rathnakara S, 这对我有用,但成功登录后,我的应用程序又被重定向到Microsoft登录页面。你能帮我吗? - Lalit Kushwah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接