【问题标题】:OWIN - Authentication.SignOut() doesn't remove cookiesOWIN - Authentication.SignOut() 不会删除 cookie
【发布时间】:2015-12-12 02:33:42
【问题描述】:

我在 Azure 中有一个带有 AD 身份验证的 MVC Web 应用程序。当我在本地运行网站时,它使用 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();
    }
}

我发现一个帖子 here 有类似的问题,并尝试了它的建议,但它对我不起作用。

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

【问题讨论】:

    标签: asp.net-mvc azure


    【解决方案1】:

    我已经弄清楚了问题所在。我创建的带有 AD 身份验证的 Azure 中开箱即用的 MVC Web 应用程序使用 AspNet cookie。 GetOwinContext().Authentication.SignOut 清除。这对我来说在本地主机上工作得很好。当我将其部署到 Azure 并在 new Azure 门户中配置网站以使用 AD 身份验证时,出现了问题。它似乎将网站转换为 Azure 应用服务。现在 cookie 是 AppServiceAuthSession cookie - 不再是 AspNet cookie。因此,注销不再起作用。

    这是我与之合作的 Microsoft 代表的回复:

    我对此进行了更多研究,并与 Azure AD 团队和 Azure 网站团队进行了交谈。显然,新的门户设置会为您处理所有身份验证组件。因此,实际上您有两种方法可以针对您的网站设置 Auzre AD 身份验证。您可以像在 Out of the Box ASP.NET MVC 项目中看到的那样通过代码来完成此操作,您可以在其中访问 AccountController。

    或者另一种方法是让 Azure 为您处理它,方法是在新的 Azure 门户中启用该设置。当您让新的 Azure 门户执行此操作时,它会使用不同的会话 cookie 名称和不同的注销逻辑。自动身份验证似乎不能很好地与编码的注销逻辑配合使用。

    所以你的解决方法是正确的。您基本上有两种解决方法来启动并运行支持 Azure AD 身份验证的 MVC 应用程序:

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

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 2016-04-16
      • 2016-06-03
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2012-05-23
      • 2013-05-28
      相关资源
      最近更新 更多