OWIN - Authentication.SignOut()不会删除cookie

use*_*871 7 asp.net-mvc azure

我在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)

我发现这里的帖子有类似的问题,并尝试了它的建议,但它对我不起作用.

还有其他人遇到过这个问题吗?

use*_*871 2

我已经弄清楚问题是什么了。我创建的 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
    \n
  1. 通过代码创建支持 AAD 身份验证的 MVC 应用程序。手动将应用程序添加到该 Azure AD 租户应用程序列表以设置信任。通过 MVC 应用程序中的代码处理登录/注销
  2. \n
  3. 创建一个没有任何身份验证逻辑的 MVC 应用程序。将其配置为通过新门户支持 Azure AD 身份验证。添加一些用于登录和注销的特定链接。对于第二种情况,我建议您下拉并使用此处的示例: https: //github.com/btardif/Websites-Authentication-Authorization。您可以看到该示例支持注销链接,但它利用了该新门户中的新身份验证/授权设置。将该示例部署到新网站,在新门户中启用身份验证设置,您\xe2\x80\x99 将看到注销正常工作并正确删除这些身份验证会话 cookie。
  4. \n
\n