在我的Asp.Net MVC应用程序中,我最近集成了Azure AD身份验证。登录操作正常,但我无法正确注销。我只想让用户从我的应用程序中注销,而不是"注销";单签出";。我读到的所有内容都是关于单次注销的。以下是我尝试过的:
-
放弃会话并重定向到登录页面。在Html.AntiForgeryToken((语句的登录页面上引发了与丢失声明相关的错误
Session.Abandon(); return RedirectToAction("Index", "Login");
-
这与#1 有相同的错误
FederatedAuthentication.SessionAuthenticationModule.SignOut(); FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie(); return RedirectToAction("Index", "Login");
-
这需要用户单次注销
HttpContext.GetOwinContext().Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
以下是@Jan Hajek在这里建议的一些解决方案
-
确保Azure AD在.NET应用程序中列为已连接服务。这将防止一次注销。
-
我们可以在
controller
中创建单个注销端点,如下示例所示
return SignOut(
new AuthenticationProperties
{
RedirectUri = callbackUrl,
},
CookieAuthenticationDefaults.AuthenticationScheme
);
注意:-当我们注销时,它会清除Cookie,这是正确的,因为用户会话(在Razor Pages和MVC的情况下(存储在Cookie中。
此外,我们可以将redirect logout url
(例如-"<your-doman>/.auth/logout
"(设置为应用程序本身的主页,应用程序在主页上请求再次登录。
有关更多信息,请参阅以下链接
MS BLOG:扩展应用程序服务身份验证/授权
所以线程
MS DOC:在Azure应用程序服务身份验证中自定义登录和注销