![](/img/trans.png)
[英]Login redirect a user with Azure AD B2C from an Angular application and msal.js
[英]Azure AD B2C with MSAL and Angular redirects to login page immediately after logging into the app
我有一個使用ASP.NET核心Web API的Angular應用程序。 我使用ADAL進行身份驗證,但現在應用程序也應該可供客戶訪問,因此我刪除了ADAL代碼並添加了與MSAL相關的代碼。 我創建了一個Azure AD B2C租戶,並在應用程序中使用了這些憑據。 當我現在運行應用程序時,我被重定向到Microsoft登錄頁面,這是完美的。 但是當我在輸入電子郵件和密碼后登錄時,它會登錄到應用程序並立即將我重新定向到登錄頁面。 我沒有時間在Dev工具中調試問題。 msal.service.ts文件中的代碼如下
private applicationConfig: any = {
clientID: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
authority: 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
b2cScopes: ['XXXXXXXXXXXXXXXXXXXXXXXXX'],
redirectUrl: 'XXXXXXXXXXXXXXXXXXXXXXXX'
};
private app: any;
public user: any;
constructor() {
this.app = new UserAgentApplication(this.applicationConfig.clientID, this.applicationConfig.authority,
(errorDesc, token, error, tokenType) => {
console.log(token);
}, { redirectUri: this.applicationConfig.redirectUrl });
// this.app.redirectUri=this.applicationConfig.redirectUrl;
}
public login() {
let tokenData = '';
this.app.loginRedirect(this.applicationConfig.b2cScopes).then(data => {tokenData = data; });
}
public getUser() {
const user = this.app.getUser();
if (user) {
return user;
} else {
return null;
}
}
public logout() {
this.app.logout();
}
public getToken() {
return this.app.acquireTokenSilent(this.applicationConfig.b2cScopes)
.then(accessToken => {
console.log(accessToken);
return accessToken;
}, error => {
return this.app.acquireTokenPopup(this.applicationConfig.b2cScopes)
.then(accessToken => {
return accessToken;
}, err => {
console.error(err);
});
});
}
auth.service.ts中的代碼如下所示
constructor(private msalService: MSALService, public jwtHelper: JwtHelper) { }
public getToken(): string {
const token: string = window.sessionStorage.getItem('msal.idtoken');
return token;
}
public login() {
const token = this.getToken();
if (token == null || token === undefined || token === 'null') {
this.msalService.login();
}
}
public getTokenDecoded(): any
{
return this.jwtHelper.decodeToken(this.getToken());
}
我花了很多時間在這上面,但無法確定問題的確切原因。 這個你能幫我嗎
要調試這些問題,請在控制台和網絡選項卡中打開preserve log
:
URL中可能返回了一個錯誤,該錯誤應該在網絡選項卡中可見。
例如:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.