我创建了一个仅属于我们组织的spa
应用程序,但在请求代码时出现了问题。我该如何解决?
我创建了一个仅属于我们组织的spa
应用程序,但在请求代码时出现了问题。我该如何解决?
Web
,您输入 https://<your app's name>.azurewebsites.net/.auth/login/aad/callback
;而对于 SPA
,您只需输入 https://skywalk.azurewebsites.net
。然而,在发布默认的 Blazor WASM 应用程序后,我现在看到了它的 Index
页面,但没有应用样式。这是怎么回事?! - Yoda如果你正在开发一个纯React、Angular、Vue等SPA网站,而不是使用Java、C#、PHP等SSR网站或混合型网站(如Nuxt、Next等),并且你需要access_token而不是授权码(SSR),那么你应该按照以下步骤进行操作:注意:某些IAM平台正在弃用隐式授权流程
https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
添加一个网络平台类型的SPA,并将重定向到您的SPA
第三步在这一步中,您应该能够获取此类登录所需的经典值(如google、facebook、microsoft等):clientid、clientsecret、reduirect_uri、tenant等。
第五步https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&response_type=token
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&scope=openid
&response_mode=query
如果一切正常,所有最终用户在登录步骤时都会收到类似以下内容的提示:
是的,这是另一个“Azure功能”。不管怎样,是谁告诉你使用微软工具的?为了避免这种情况,通过管理员的帮助,批准它或按照这些无限步骤将您的应用标记为已验证。不知道我是否来晚了,但我在我的React SPA应用程序中遇到了同样的问题,解决方案不是将我的应用程序更改为Web in Portal,而是解决实际问题,即我的请求中没有证明密钥。我通过创建帮助函数并将其添加到我发送用户进行身份验证的URL中来解决它:
function buildAuthorizationUrl(codeChallenge) {
const clientId = "YOUR_CLIENT_ID":
const redirectUri = encodeURIComponent("http://localhost:5173/auth/");
// Change to url you need
const responseType = "code";
const scope = encodeURIComponent("openid profile offline_access User.Read");
const state = encodeURIComponent("my_custom_state");
return `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${clientId}&response_type=${responseType}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}&code_challenge=${codeChallenge}&code_challenge_method=S256`;
}
async function generateCodeVerifier() {
const array = new Uint8Array(32);
crypto.getRandomValues(array);
return Array.from(array)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
async function generateCodeChallenge(codeVerifier) {
const encoder = new TextEncoder();
const data = encoder.encode(codeVerifier);
const digest = await crypto.subtle.digest("SHA-256", data);
const base64Url = (arrayBuffer) => {
const base64 = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
return base64.replace("+", "-").replace("/", "_").replace(/=+$/, "");
};
return base64Url(digest);
}
然后当用户点击登录按钮时,我添加了这个逻辑:
const handleClick = async () => {
const codeVerifier = await generateCodeVerifier();
const codeChallenge = await generateCodeChallenge(codeVerifier);
sessionStorage.setItem("code_verifier", codeVerifier);
const authUrl = buildAuthorizationUrl(codeChallenge);
window.location.href = authUrl;
};
当然,如果您没有处理常见的租户,那么在buildAuthorizationUrl函数中也应进行更改。
code_verifier
,然后使用code_verifier
生成code_challenge
。authorization_code
时添加code_challenge
,以及code_challenge_method:S256
(SHA256)。
对于令牌请求,需要发送code_verifier
。
以下是Curl请求示例:
授权
curl --location --request GET 'https://login.microsoftonline.com/<tenat_id>/oauth2/v2.0/authorize?client_id=<client_id>&redirect_uri=<redirect_uri>&scope=<scope>&response_type=code&response_mode=query&sate=demo&code_challenge=<code_challenge>&code_challenge_method=S256'
令牌
curl --location --request POST 'https://login.microsoftonline.com/<tenat_id>/oauth2/v2.0/token --form 'code_verifier=<code_verifier>
@azure/msal-browser
和@azure/msal-react
时,我不得不将我的 Azure 应用程序平台切换回 SPA,以使身份验证成功。 - w. Patrick Gale