我在有角度的6 SPA上使用msal.js来处理身份验证时,遇到了一些问题:首先,我找不到关于如何处理lib错误的清晰示例,因此我左右选择了东西,然后到达到下一个结果:
@Injectable()
export class AuthenticationService {
_clientApplication: Msal.UserAgentApplication;
_clientApplicationPR: Msal.UserAgentApplication;
private _authority: string;
constructor(private apiService: ApiService, private backendRoutes: BackendRoutes) {
this._authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.signUpSignInPolicy}`;
this._clientApplication =
new Msal.UserAgentApplication(
environment.clientID,
this._authority,
this._authCallback,
{
cacheLocation: 'localStorage',
redirectUri: window.location.origin
});
}
private _authCallback(errorDesc: any, token: any, error: any, tokenType:
any) {
console.log("in call back");
if (token) {
this.addUser();
} else {
// console.log(`${error} - ${errorDesc}`);
if (errorDesc.indexOf("AADB2C90118") > -1) {
//Forgotten password
this._clientApplicationPR = new Msal.UserAgentApplication(
environment.clientID,
`https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.passResetPolicy}`,
this._authCallback,
{
cacheLocation: 'localStorage',
redirectUri: …
Run Code Online (Sandbox Code Playgroud) 再会,
我正在尝试在我的应用程序中实现 MSAL.js。基本上,我能够实现 msal.js 并使用 AD 中的电子邮件登录。我还配置了 msal 来保存我的tokenid
并accessToken
在我的localStorage
它accessToken
在我的后端工作,因为我使用 Postman 软件测试了它。但在我的应用程序中,我不知道如何获取并将其放入我的Axios
标题中,因为key
显示localStorage
如下:
其中它Key
在 localStorage 中有一个对象。
{"authority":"https://login.microsoftonline.com/TENANTID/","clientId":"CLIENT_ID","scopes":"CLIENT_ID","homeAccountIdentifier":"SOME_HASH"}
Run Code Online (Sandbox Code Playgroud)
值为:
accessToken: 'TOKEN_HASH_HERE'
idToken: 'TOKEN_HASH_HERE'
expiresIn: 'TOKEN_HASH_HERE'
homeAccountIdentifier: 'TOKEN_HASH_HERE'
Run Code Online (Sandbox Code Playgroud)
我需要获取访问令牌才能将其放入我的axios
标头中,如下所示:
axios.defaults.headers.common['Authorization'] = `Bearer ${accessTokenFromMsal}`
Run Code Online (Sandbox Code Playgroud)
先感谢您。
我正在开发一个 Angular 10 应用程序,它利用 Azure B2C 进行策略和用户管理。我在 Azure Active Directory 中将我的应用注册设置为单页应用,但未选中隐式选项。我正在使用 msal.js 2.0@azure/msal-browser
登录 B2C 并使用代码流检索 ID 和访问令牌。我设置了我的配置,创建了 msal 对象,定义了重定向承诺,然后使用适当的用户范围调用 loginRedirect。页面正确重定向。
但是,在我登录后,tokenResponse 返回为空。我曾尝试更改权限和范围,但它总是返回为空。如何让handleRedirectPromise
返回有效的令牌响应?
这是我的代码:
private msalConfig: Msal.Configuration = {
auth: {
clientId: xxxx-xx-xx-xx-xxxxx,
authority: 'https://login.microsoftonline.com/common',
redirectUri: 'https://localhost:4200'
},
cache: {
cacheLocation: 'sessionStorage',
storeAuthStateInCookie: false
},
};
private loginRequest: Msal.RedirectRequest = {
scopes: ['user.read'],
};
const msalInstance = new Msal.PublicClientApplication(this.msalConfig);
msalInstance
.handleRedirectPromise()
.then((tokenResponse: Msal.AuthenticationResult) => {
let accountObj = null;
if (tokenResponse !== null) {
accountObj = tokenResponse.account; …
Run Code Online (Sandbox Code Playgroud) 我已复制此存储库中的示例应用程序以尝试实现单点登录:https://github.com/Azure-Samples/ms-identity-javascript-v2。
在 authConfig.js 文件中的 msalConfig 的 auth 对象中,我替换了应用程序的 clientId 和权限值,并使用了“http://localhost:5500”的redirectUrl 值,因为我需要首先在本地进行测试。
我收到了相应的 SSO 弹出窗口,该弹出窗口会进行处理,然后因以下错误而停止:
AADSTS900971:未提供回复地址。
是否必须有服务器端设置才能使其工作?可以从我的机器上的本地主机(或 127.0.0.1)进行测试吗?
我正在一个带有几个Angular 4 SPA的系统上工作,每个SPA都与单独的Asp.NET Core 2.0 WebAPI进行通信。
Azure AD B2C用作每个SPA / API的身份服务,而MSAL.js用作Angular SPA的一部分。
我们希望支持单一登录,以防止用户从一个SPA切换到另一个SPA时需要重新输入凭据。
是否可以使用msal.js v0.1.7获得单点登录行为?
是否可以在不重定向的情况下注销?
每次我注册用户时,我都需要验证用户是否在外部数据库中(带有邀请令牌的数据库)。我是在注册完成后才做的。当我发现用户不应该被允许登录时,我需要调用logout
以清除登录弹出窗口中的 cookie/存储(如果我在再次打开 singin 弹出窗口后不这样做,azure 会尝试自动登录用户并且不允许,例如,重新注册)。在我的情况下,页面的刷新是不必要的,因为它是一个注册过程,我已经在登录页面进行刷新。
我使用 MSAL JS 来验证用户身份,然后调用 acquireTokenPopup(scopes) 来获取访问令牌。
我正在获取访问令牌,但无法使用它,因为它显示Invalid Signature。(也在 jwt.io 中检查过 - 同样的错误)
在论坛上我发现这是由于 Graph 添加了随机数造成的。解决方案是什么?请帮忙。
以下是代码。
tenantConfig = {
scopes: ["directory.read.all"]
};
Run Code Online (Sandbox Code Playgroud)
this.clientApplication.acquireTokenSilent(this.tenantConfig.scopes).then( 函数 (accessToken) {
},
function (error) {
console.log(error);
this.clientApplication
.acquireTokenPopup(this.tenantConfig.scopes)
.then(
function (accessToken) {
console.log("access token " + accessToken);
},
function (error) {
alert(error);
}
);
}
);
Run Code Online (Sandbox Code Playgroud) 我正在使用一个create-react-app
基础项目,用于react-aad-msal
在客户端上向 Azure AD 进行身份验证。这工作正常,我可以在身份验证后检索基本的用户详细信息。但是,我想将对整个站点的访问限制为特定 AD 组的成员。我的所有研究都表明需要使用 Microsoft Graph API 来查询 AD,但在我看来,要做一些应该更简单的事情需要做很多工作。
我的问题是是否有更简单的方法来做到这一点?我可以通过 MSAL 来利用索赔来实现这一目标吗?如果没有,是否有一个可以很好地react-add-msal
简化流程的库?
我知道这应该很简单,但如果能提供一点帮助,我们将不胜感激;我们对使用工业强度打字稿包都有些陌生。
我们正在构建一个 Angular 应用程序并使用 @azure/msal-angular 库,它在大多数情况下都可以正常工作;遵循教程和示例,通常都有意义。
除了在广播“msal:acquireTokenFailure”事件时要实现什么?
在 AppComponent 的 ngOnInit() 方法中,我们有这一行
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
// ToDo: What should be implemented here?
});
Run Code Online (Sandbox Code Playgroud)
在 GitHub 页面的一些帖子中,项目贡献者提出了一些类似的建议
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
this.subscription.unsubscribe();
this.authService.loginRedirect();
});
Run Code Online (Sandbox Code Playgroud)
就我而言,这将重定向到 AzuerAD 登录屏幕,但会丢失我们试图为其获取令牌的底层调用详细信息。
看起来更有用的(在伪代码中)是这样的
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
this.subscription.unsubscribe();
if (isIE) {
this.authService.acquireTokenRedirect(userRequest);
} else {
this.authService.acquireTokenPopup(userRequest);
}
}); …
Run Code Online (Sandbox Code Playgroud) 这是我正在编写的代码。例如,我在同一应用程序上使用MSAL
两个应用程序,请参阅下面的代码片段。SSO
domain
https://some-domain.com/app1
https://some-domain.com/app2
应用程序 1 似乎没问题,它允许用户正确登录。但是,在应用程序 2 上,当我重新加载页面时,它会抛出error
MSAL:InteractionRequiredAuthError:no_tokens_found:在缓存中找不到刷新令牌。请登录。
我已经使用过instance.acquireTokenRedirect
,acquireTokenSilent
但identityInstance.loginRedirect()
似乎没有任何作用。有什么想法请分享。谢谢。
const [userName, setUsername] = useState<string | undefined>()
useEffect(() => {
const fetchDetaiils = async () => {
if (inProgress === InteractionStatus.None) {
try {
const signedInUser = identityInstance.getAllAccounts()[0]
const resp = await identityInstance.acquireTokenSilent({
scopes: ['user.read'],
account,
})
const token: Token = resp?.idTokenClaims
setUsername(token.email)
} catch (err: unknown) {
if (err instanceof Error) {
console.log(err)
if (err?.name === 'InteractionRequiredAuthError') { …
Run Code Online (Sandbox Code Playgroud) msal.js ×10
azure-ad-b2c ×4
reactjs ×3
angular ×1
axios ×1
azure ×1
javascript ×1
localhost ×1