angular - 如何在 ionic 项目中使用 Ionic native - MS Adal?

标签 angular typescript ionic-framework adal

我是 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: Wrong type for parameter "userId" of AuthenticationContext.acquireTokenAsync:Expected String,but got Function.

最佳答案

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

在 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));

关于angular - 如何在 ionic 项目中使用 Ionic native - MS Adal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51068925/

相关文章:

angular - NgZone/Angular2/Ionic2 类型错误 : Cannot read property 'run' of undefined

java - 为 cordova (Android) 开发插件时无法找到源 java 类

angular - 无法使用 typescript 和 Angular 为 Jasmine 添加自定义匹配器

angular - 无法根据路由请求在 Angular 中显示微调器

javascript - Visual Studio Typescript - 重复的全局标识符

javascript - 使用 flow.js + ng-flow 将文件上传到 WebAPI 2

ios - 如果健康应用程序尚未设置,如何使用 healthkit?

angular - Ng2-smart-table:是否可以显示和隐藏列?

javascript - 将对象数组转换为不同的对象数组 - Javascript/Angular

typescript - 如何使用 Cypress 拖放自定义文件