【发布时间】:2018-03-11 04:15:58
【问题描述】:
我在 Azure 中运行了 Asp.Net 核心后端。 在 localhost 上运行的 HTML/JS 前端,使用 CORS 与后端通信
当前端和后端都在本地主机中,或者它们都在 Azure 中时,身份验证有效 -> Azure AD 应用程序设置正确。
这是我的登录方式:
[Route("/api/[controller]")]
public class AccountController : Controller
{
[HttpGet]
public IActionResult Index()
{
return Json(new AccountInfoViewModel
{
IsAuthenticated = User.Identity.IsAuthenticated,
UserName = User.Identity.Name,
Roles = new string[0],
LoginUrl = Url.Action(nameof(Login), null, null, null, Request.Host.Value),
LogoutUrl = Url.Action(nameof(Login), null, null, null, Request.Host.Value),
});
}
[HttpGet("login")]
public async Task Login(string returnUrl)
{
await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = returnUrl });
}
[HttpGet("logoff")]
public async Task LogOff()
{
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
[HttpGet("endsession")]
public async Task EndSession()
{
// If AAD sends a single sign-out message to the app, end the user's session, but don't redirect to AAD for sign out.
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}
所以从本地主机,然后我重定向到:
https://myapp.azurewebsites.net/Account/Login?returnUrl=localhost:12345
触发Login 操作并将我重定向到我的Azure AD SSO 页面,登录后将我重定向回本地主机。但是,对后端的请求仍然没有经过身份验证。
重要: 当我从登录操作中删除 redirectUrl 时,我被重定向到后端根目录而不是原始来源 (localhost)。来自该来源(后端)的任何请求都经过身份验证。
【问题讨论】:
-
嗨@Liero,你能分享更多信息吗?查看这些请求的所有响应标头确实会有所帮助。 (原始的和重定向的)
-
好吧,有了
xhr.withCredentials = true;,我已经设法让它在 Edge 中工作,看看我的回答。但是,它仍然无法在 Chrome 中运行。 Edge 发送 2 个 cookie:ARRAfinity=... AspNetCore.Cookies=...。 Chrome 只发送 ARRAfinity,我不知道为什么
标签: c# asp.net-core cors azure-active-directory openid-connect