使用 C# 从 ADFS 注销
Logout from ADFS using c#
我使用 ADFS 创建了一个 asp.net 网络表单应用程序。使用模板附带的默认方法可以完美地登录和注销。
模板中包含的注销按钮方法示例
protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
{
// Redirect to ~/Account/SignOut after signing out.
string callbackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Response.ApplyAppPathModifier("~/Account/SignOut");
HttpContext.Current.GetOwinContext().Authentication.SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
WsFederationAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
}
我已经设置了一个计时器,在达到零时我尝试使用上面的代码注销用户,但它没有抛出 work.No 错误。
有什么关于如何在此处执行注销的建议吗?
你有没有试过上面你直接贴出来的没有定时器的代码?它有效吗?
另外,尝试实现下面的代码,看看它是否有效。
public void LogOut()
{
var module = FederatedAuthentication.WSFederationAuthenticationModule;
module.SignOut(false);
var request = new SignOutRequestMessage(new Uri(module.Issuer), module.Realm);
Response.Redirect(request.WriteQueryString());
}
对我有用的是在超时时调用隐藏按钮的点击事件,这反过来导致下面的代码 运行。
// Redirect to ~/Account/SignOut after signing out.
string callbackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Response.ApplyAppPathModifier("~/Account/SignOut");
HttpContext.Current.GetOwinContext().Authentication.SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
WsFederationAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
ADFS 是负责验证用户身份和管理用户会话的服务器。 website/form 正在使用此服务。使用此服务的站点无法完全控制它是有道理的。从 ADFS 服务器注销用户并让该服务器为您完成繁重的工作对我来说更有意义。
注意 ADFS 服务器保持用户登录到 ADFS 服务器,并注意当用户请求访问资源时,这会显示在 access_token 中。它们是不同的东西。通常,当使用身份服务器等产品将某人注销时,为了注销,您需要做两件事:
- 撤销访问令牌
- 在身份验证服务器上注销,(如果
这是理想的,有人可能会争辩说这是不可取的)
请注意会话和令牌之间的明确区别。您会注意到这些概念也存在于 ADFS 中。在快速 google 搜索后,您会发现 WebSSOLifetime 和 TokenLifetime 之间的区别。我建议将它们配置为使令牌和会话无效,从而在 x 分钟后将用户注销。
希望对您有所帮助。
我使用 ADFS 创建了一个 asp.net 网络表单应用程序。使用模板附带的默认方法可以完美地登录和注销。
模板中包含的注销按钮方法示例
protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
{
// Redirect to ~/Account/SignOut after signing out.
string callbackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Response.ApplyAppPathModifier("~/Account/SignOut");
HttpContext.Current.GetOwinContext().Authentication.SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
WsFederationAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
}
我已经设置了一个计时器,在达到零时我尝试使用上面的代码注销用户,但它没有抛出 work.No 错误。
有什么关于如何在此处执行注销的建议吗?
你有没有试过上面你直接贴出来的没有定时器的代码?它有效吗?
另外,尝试实现下面的代码,看看它是否有效。
public void LogOut()
{
var module = FederatedAuthentication.WSFederationAuthenticationModule;
module.SignOut(false);
var request = new SignOutRequestMessage(new Uri(module.Issuer), module.Realm);
Response.Redirect(request.WriteQueryString());
}
对我有用的是在超时时调用隐藏按钮的点击事件,这反过来导致下面的代码 运行。
// Redirect to ~/Account/SignOut after signing out.
string callbackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Response.ApplyAppPathModifier("~/Account/SignOut");
HttpContext.Current.GetOwinContext().Authentication.SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
WsFederationAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
ADFS 是负责验证用户身份和管理用户会话的服务器。 website/form 正在使用此服务。使用此服务的站点无法完全控制它是有道理的。从 ADFS 服务器注销用户并让该服务器为您完成繁重的工作对我来说更有意义。
注意 ADFS 服务器保持用户登录到 ADFS 服务器,并注意当用户请求访问资源时,这会显示在 access_token 中。它们是不同的东西。通常,当使用身份服务器等产品将某人注销时,为了注销,您需要做两件事:
- 撤销访问令牌
- 在身份验证服务器上注销,(如果 这是理想的,有人可能会争辩说这是不可取的)
请注意会话和令牌之间的明确区别。您会注意到这些概念也存在于 ADFS 中。在快速 google 搜索后,您会发现 WebSSOLifetime 和 TokenLifetime 之间的区别。我建议将它们配置为使令牌和会话无效,从而在 x 分钟后将用户注销。
希望对您有所帮助。