[英]Azure AD B2C with MSAL and Angular redirects to login page immediately after logging into the app
I have an Angular app that uses ASP.NET core Web API. 我有一个使用ASP.NET核心Web API的Angular应用程序。 I used ADAL for authentication but now the app should also be accessible to customers and hence I removed the ADAL code and added MSAL related code.
我使用ADAL进行身份验证,但现在应用程序也应该可供客户访问,因此我删除了ADAL代码并添加了与MSAL相关的代码。 I created an Azure AD B2C tenant and used those credentials in the app.
我创建了一个Azure AD B2C租户,并在应用程序中使用了这些凭据。 When I run the app now, I am redirected to Microsoft login page which is perfect.
当我现在运行应用程序时,我被重定向到Microsoft登录页面,这是完美的。 But when I login after entering email and password, it logs into the app and immediately redirects me to the login page again.
但是当我在输入电子邮件和密码后登录时,它会登录到应用程序并立即将我重新定向到登录页面。 I don't get time to even debug the issue in Dev tools.
我没有时间在Dev工具中调试问题。 The code in msal.service.ts file is below
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);
});
});
}
The code in auth.service.ts is shown below 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());
}
I spent a lot of time on this but unable to identify the exact cause of the issue. 我花了很多时间在这上面,但无法确定问题的确切原因。 Please help me out with this
这个你能帮我吗
To debug these problems turn on preserve log
in the console and network tabs: 要调试这些问题,请在控制台和网络选项卡中打开
preserve log
:
There is probably an error returned in the URL which should be visible in the network tab. URL中可能返回了一个错误,该错误应该在网络选项卡中可见。
ex: 例如:
https://jwt.ms/#error=server_error&error_description=AADB2C90161%3a+A+self-asserted+send+response+has+failed+with+reason+%27Internal+Server+Error%27.%0d%0aCorrelation+ID%3a+bc145550-912a-4f35-b833-334cf1ace96d%0d%0aTimestamp%3a+2018-01-08+01%3a48%3a43Z%0d%0a
https://jwt.ms/#error=server_error&error_description=AADB2C90161%3a+A+self-asserted+send+response+has+failed+with+reason+%27Internal+Server+Error%27.%0d%0aCorrelation+ID%图3a + bc145550-912a-4f35-b833-334cf1ace96d%0D%0aTimestamp%3A + 2018年1月8日+ 01%3a48%3a43Z%0D 0A%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.