大佬教程收集整理的这篇文章主要介绍了登录从Angular应用程序和msal.js重定向Azure AD B2C的用户,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在拥有的是我在我的应用程序中注入的以下身份验证服务,该服务应该负责所有身份验证.
import { Injectable } from '@angular/core'; import * as Msal from 'msal'; @Injectable() export class Authenticationservice { private tenantConfig = { tenant: "example.onmicrosoft.com",clientID: 'redacted (guid of the client),signUpSignInPolicy: "b2c_1_signup",b2cScopes: ["https://example.onmicrosoft.com/demo/read","openid"] } private authority = "https://login.microsoftonline.com/tfp/" + this.tenantConfig.tenant + "/" + this.tenantConfig.signUpSignInPolicy; private clientApplication: Msal.UserAgentApplication; constructor() { this.CLIENtApplication = new Msal.UserAgentApplication(this.tenantConfig.clientID,this.authority,this.authCallBACk); } public login(): void { this.CLIENtApplication.loginRedirect(this.tenantConfig.b2cScopes); } public logout(): void { this.CLIENtApplication.logout(); } public isOnline(): @R_618_8487@an { return this.CLIENtApplication.getUser() != null; } public getUser(): Msal.User { return this.CLIENtApplication.getUser(); } public getAuthenticationToken(): Promise<String> { return this.CLIENtApplication.acquireTokenSilent(this.tenantConfig.b2cScopes) .then(token => { console.log("Got silent access token: ",token); return token; }).catch(error => { console.log("Could not silently retrieve token from storage.",error); return this.CLIENtApplication.acquireTokenPopup(this.tenantConfig.b2cScopes) .then(token => { console.log("Got popup access token: ",token); return token; }).catch(error => { console.log("Could not retrieve token from popup.",error); this.CLIENtApplication.acquireTokenRedirect(this.tenantConfig.b2cScopes); return Promise.resolve(""); }); }); } private authCallBACk(errorDesc: any,token: any,error: any,tokenType: any) { console.log(CallBACk') if (token) { console.log("Id token",token); } else { console.log(error + ":" + errordesc); } this.getAuthenticationToken(); } }
但它不起作用.我正确地获得了ID令牌,并且它是有效的,因此“Id令牌”确实会收到我可以使用的值,但是在有限的时间内.
问题是当我尝试获取身份验证令牌时.对acquireTokenSilent的第一次@L_618_16@一个错误,指出:由于超时,令牌更新操作失败:null.
然后,我得到一个弹出窗口,询问用户名和密码,一段时间后消失,我收到一条错误消息,说用户没有现有会话,请求提示参数的值为’无’.
编辑:
所以,我想我明白了究竟发生了什么,并在一个示例应用程序上重现问题,你可以在这里找到:https://github.com/Gimly/NetCoreAngularAzureB2CMsal
如果从主页连接然后转到fetchData页面(具有天气预报的页面),您可以看到由acquireTokenSilent正确兑换身份验证令牌(打开浏览器控制台以获取所有日志).但是,如果直接在fetchData上刷新页面,您可以看到我描述的相同行为,其中acquireTokenSilent失败并显示有关超时的错误.
我最好的猜测是,出于某种原因,即使getUser返回正确的值,在调用getAuthenticationToken之前msal还没有完全初始化,这就是它完全失败的原因.
现在真正的问题是……在尝试获取令牌之前,如何确保它已完全初始化?
1)msal.js中的一个错误导致初始化过程在构造函数调用结束时没有完全完成,我在完全初始化之前调用了acquireTokenSilent().这是在最新版本中修复的(截至编写时,仍在项目的开发分支中). See this GitHub issue for more info.
2)我没有修复重定向URI,这导致库将当前URL作为重定向URI,因为它不在允许的重定向URI列表中,所以会失败.
修复这两个问题后,我在我的问题中发布的代码按预期工作.
以上是大佬教程为你收集整理的登录从Angular应用程序和msal.js重定向Azure AD B2C的用户全部内容,希望文章能够帮你解决登录从Angular应用程序和msal.js重定向Azure AD B2C的用户所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。