【问题标题】:How to use Ionic native - MS Adal in ionic project?如何在离子项目中使用 Ionic native - MS Adal?
【发布时间】:2018-12-06 17:05:25
【问题描述】:

我是 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”的类型错误:预期的字符串,但得到了函数。

【问题讨论】:

  • 我也有同样的问题。所以我开始赏金所以有人帮助你。
  • 你能在stackblitz.com创建演示吗?
  • @KarnanMuthukumar 这不应该发生。您可以通过传递 userId(可选)参数值 null 来尝试吗? authContext.acquireTokenAsync('graph.windows.net', '[My-appID]', 'localhost:8000', null)
  • 好的,让我们再试一次。感谢您的回复
  • 我更新了你的代码。我得到了这种类型的错误。 AuthenticationContext.acquireTokenAsync 的参数“extraQueryParameters”的类型错误:预期的字符串,但得到了函数。

标签: angular typescript ionic-framework adal


【解决方案1】:

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

在 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 是什么。
  • 比如在 createauthencationcontext 和 acuiredtokenasync 中传递的 url 是什么?
  • 是的。但我想在 ionic 3 中进行窗口身份验证
  • 嗨@Rathnakara S,这对我有用,但成功登录后,我的应用程序再次重定向到 Microsoft 登录页面。你能帮我吗?
猜你喜欢
  • 2017-12-23
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
相关资源
最近更新 更多