MSAL Azure AD登录后调用/api/authentication/SetAzureUser端点遇404错误
问题描述
我正尝试使用UserAgentApplication.loginPopup对Azure AD用户进行验证。我们正从ASP.NET MVC应用迁移至Vue.js前端+ASP.NET API后端架构,计划将MSAL获取的accessToken传递给后端以验证用户权限。本地运行时一切正常,但部署到服务器后,调用'/api/authentication/SetAzureUser'端点时出现404错误。
前端代码(route.js)
var myMSALObj; const getLoginPopup = (request) => { return myMSALObj.loginPopup(request); } const getTokenPopup = (request) => { return myMSALObj.acquireTokenSilent(request) .catch(() => { // 当静默调用失败时,回退到交互式调用 return myMSALObj.acquireTokenPopup(request) .then(tokenResponse => { return tokenResponse; }).catch(error => { console.log(error); }); }); } const azureUserAuthentication = async () => { const utilityStore = useUtilityStore(); try { const msalConfig = { auth: { clientId: "clientId", authority: "https://login.microsoftonline.com/", redirectUri: `${window.location.protocol}//${window.location.host}/api/authentication/AzureAuthenticationCallback`, }, cache: { cacheLocation: "sessionStorage", // 配置缓存存储位置 storeAuthStateInCookie: false, // 若在IE11或Edge中遇到问题,请设为"true" } }; const loginRequest = { scopes: ["openid", "profile", "User.Read.All"] }; const tokenRequest = { scopes: ["Mail.Read"] }; myMSALObj = new UserAgentApplication(msalConfig); var loginPopupResponse = await getLoginPopup(loginRequest); if (loginPopupResponse != null && myMSALObj.getAccount()) { var tokenResponse = await getTokenPopup(tokenRequest); if (tokenResponse != null) { // 此axios调用出现404错误 var setAzureUserResponse = await axios({ method: 'get', url: '/api/authentication/SetAzureUser', contentType: "application/json", withCredentials: true, params: { accessToken: tokenResponse.accessToken } }); if (!setAzureUserResponse.succeeded) { window.document.title = "Unauthorized Access"; return { name: 'unauthorized-access' } } else { utilityStore.$patch({ showProcessingOverlay: false }); } } } } catch (ex) { console.log(ex); } }
后端API端点代码
[HttpGet("SetAzureUser")] public JsonResult SetAzureUser(string accessToken) { return Json(_authenticationService.AuthenticateAzureUser(_stateManagement.BuildBaseParameters(), accessToken)); }
内容的提问来源于stack exchange,提问作者Ryan
相关产品推荐
相关产品推荐

