b2c msal 用户没有现有的会话和请求提示参数



当我通过loginRedirect登录时,我可以看到 MSAL 令牌已填充在重定向中,但是在尝试使用该令牌时,我收到此错误:

无法以静默方式从存储中检索令牌。

AADB2C90077:用户没有现有会话,请求提示参数的值为"无",在通过连接到 Azure B2C 的 Angular SPA 应用成功登录后。

我在 GitHub 问题中发现了类似的东西,然后在由于没有令牌而被迫执行tokenPopup()后出现错误后对另一个问题发表了评论:

"此应用程序对此 Web 资源没有足够的权限来执行该操作"

他们可能没有关系,但他们都在阻止我

在此代码中,loginredirect工作,我可以在本地存储中看到令牌,然后下次使用该令牌时,我会收到控制台日志authCallback-err然后记录"无法静默地从存储中检索令牌..."和错误首先提到。

@Injectable()
export class MsalService {
private access_token: string;
private tenantConfig = {
tenant: environment.tenant,
clientID: environment.clientID,
signUpSignInPolicy: environment.signUpSignInPolicy,
b2cScopes: environment.b2cScopes
};
private authority = "https://login.microsoftonline.com/tfp/" +
this.tenantConfig.tenant +
"/" +
this.tenantConfig.signUpSignInPolicy;
private clientApplication: Msal.UserAgentApplication;
private authCallback(errorDesc: any, token: any, error: any, tokenType: any) {
var _this = this;
console.log("authCallback");
// For loginRedirect, tokenType = "id_token". For acquireTokenRedirect, tokenType:"access_token".
if (token) {
console.log("authCallback- Id token", token);
this.access_token = token;
} else {
console.log("authCallback-err : " + error + ":" + errorDesc);
alert("error here");
}
}
constructor() {
this.clientApplication = new Msal.UserAgentApplication(
this.tenantConfig.clientID,
this.authority,
this.authCallback,
{
cacheLocation: "localStorage",
redirectUri: "https://localhost:4200/msallogin"
}
);
}
get authenticated() {
const user = this.clientApplication.getUser();
if (user) {
return true;
}
return false;
}
public loginRedirect(): void {
this.clientApplication.loginRedirect(this.tenantConfig.b2cScopes);
}
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("");
});
});
}
public login(): void {
var _this = this;
this.clientApplication.loginPopup(this.tenantConfig.b2cScopes).then(
function(idToken: any) {
_this.clientApplication
.acquireTokenSilent(_this.tenantConfig.b2cScopes)
.then(
function(accessToken: any) {
_this.access_token = accessToken;
console.log("ACCESS TOKEN: n " + _this.access_token);
},
function(error: any) {
_this.clientApplication
.acquireTokenPopup(_this.tenantConfig.b2cScopes)
.then(
function(accessToken: any) {
_this.access_token = accessToken;
},
function(error: any) {
alert("Error acquiring the popup:n" + error);
}
);
}
);
},
function(error: any) {
alert("Error during login:n" + error);
}
);
}
logout(): void {
this.clientApplication.logout();
}
isOnline(): boolean {
return this.clientApplication.getUser() != null;
}
public getUser(): string {
const user = this.clientApplication.getUser();
if (!user) {
return null;
}
return user.name;
}
}

然后,一旦我登录,重定向就会回来,我重定向到另一条角度路线。该组件注入了 MSAL 服务。

此时,我打电话给getUser(),可以看到我的用户名。然后,我单击一个按钮发出 Web 请求,将下一段代码作为拦截器发出,但由于没有令牌而失败

@Injectable() export class MSALAuthenticationHttpInterceptor 实现 HttpInterceptor { constructor(private msalService: MsalService) {}

@intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return Observable.fromPromise(
this.msalService.getAuthenticationToken()
).switchMap(token => {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
return next.handle(req);
});
}
}

我的 B2C 用作连接到 Azure 函数的邮递员应用,它肯定是 B2C 配置、我的代码或 MSAL。如果是,我无法从 B2C 策略边栏选项卡修改这些设置,因此不确定他是如何解决的。

B2c 配置列在 1 的注释中 我花了很多时间在这上面,这可能很容易,但是由于跨各种应用程序的多个范围以及范围边栏的不同问题和更改,它似乎比应有的更难

用:

Angular: "5.2.0"
MSAL: "^0.1.5"

我遇到了同样的错误。

我所做的是在登录策略的 XML 文件中添加了 2 行,这解决了我的问题。

<UserJourneyBehaviors>
<SessionExpiryType>Absolute</SessionExpiryType>
<SessionExpiryInSeconds>86400</SessionExpiryInSeconds>
</UserJourneyBehaviors>

我希望它会有所帮助。 :)

最新更新