我在Azure中有一个带有AD身份验证的MVC Web App.当我在本地运行网站时,使用Azure AD可以正常登录和退出.但我部署的Azure网站上的注销不起作用.用户仍然经过身份验证,因此SignOutCallback操作始终重定向到Home/Index.
这是我创建项目时创建的开箱即用的代码.
public class AccountController : Controller
{
/// <summary>
/// Use this method to sign into the website
/// </summary>
public void SignIn()
{
// Send an OpenID Connect sign-in request.
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
/// <summary>
/// Use this method to sign out of the website
/// </summary>
public void SignOut()
{
string callbackUrl = Url.Action("SignOutCallback", "Account", routeValues: null, protocol: Request.Url.Scheme);
Request.GetOwinContext().Authentication.SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
OpenIdConnectAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
}
/// <summary>
/// Use this method to redirect to Home page, once the request has been authenticated
/// </summary>
/// <returns>An <see cref="ActionResult"/> object.</returns>
public ActionResult SignOutCallback()
{
if (Request.IsAuthenticated)
{
// Redirect to home page if the user is authenticated.
return RedirectToAction("Index", "Home");
}
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这里的帖子有类似的问题,并尝试了它的建议,但它对我不起作用.
还有其他人遇到过这个问题吗?
我已经弄清楚问题是什么了。我创建的 Azure 中带有 AD 身份验证的开箱即用 MVC Web 应用程序使用AspNet cookie。GetOwinContext().Authentication.SignOut 清除其中。这对我来说在本地主机上工作得很好。当我将其部署到 Azure,然后在新的Azure 门户中配置网站以使用 AD 身份验证时,出现了问题。它似乎将该网站转换为Azure 应用服务。现在 cookie 是AppServiceAuthSession cookie - 不再是AspNet cookie。因此,注销不再有效。
\n\n以下是与我合作的 Microsoft 代表的回复:
\n\n我围绕此进行了更多研究,并与 Azure AD 团队和 Azure 网站团队进行了交谈。显然,新的门户设置会为您处理所有身份验证组件。因此,实际上您有两种方法来针对您的网站设置 Auzre AD 身份验证。您可以通过代码来完成此操作,就像您在开箱即用的 ASP.NET MVC 项目中看到的那样,您可以在其中访问 AccountController。
\n\n或者另一种方法是让 Azure 通过在新的 Azure 门户中启用该设置来为您处理。当您让新的 Azure 门户执行此操作时,它会使用不同的会话 cookie 名称和不同的注销逻辑。看来自动身份验证不能很好地与编码的注销逻辑配合使用。
\n\n所以你的解决方法是正确的。您基本上有两种解决方法来启动并运行支持 Azure AD 身份验证的 MVC 应用程序:
\n\n| 归档时间: |
|
| 查看次数: |
4144 次 |
| 最近记录: |