微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – OWIN – Authentication.SignOut()不会删除cookie

我在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();
    }
}

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

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

解决方法

我弄清楚了问题是什么.我创建的带有AD身份验证的Azure中开箱即用的MVC Web App使用AspNet cookie.其中GetowinContext().Authentication.SignOut清除.这对我来说在localhost上运行正常.当我将其部署到Azure然后在新的Azure门户中配置网站时,问题就出现了,以使用AD身份验证.它似乎将网站转换为Azure应用服务.现在cookie是AppServiceAuthSession cookie – 不再是AspNet cookie.因此,注销不再有效.

以下是我与之合作的Microsoft代表的回复

我对此进行了更多研究,并与Azure AD团队和Azure网站团队进行了交流.显然,新的门户设置会为您处理所有auth组件.所以你真的有两种方法可以在你的网站上设置Auzre AD auth.您可以通过代码中看到的代码来实现它,您可以在其中访问AccountController的开箱即用的ASP.NET MVC项目.

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐