ASP.NET Windows 身份验证注销

发布于 2024-07-25 10:40:24 字数 713 浏览 5 评论 0原文

在 ASP.NET 中使用 Windows 身份验证(如 web.config)时如何注销?

<authentication mode="Windows" />

我已经尝试过以下方法但未成功。 它会重定向,但不会注销用户。

void logoutButton_Click(object sender, EventArgs e) {
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    ViewState.Clear();
    FormsAuthentication.SignOut();
    Response.Redirect("/");
}

背景信息:

我必须使用 Windows 身份验证,因为我需要使用 Active Directory 模拟身份才能访问本地文件。 而且我无法使用表单身份验证进行模拟,因为 HttpContext.Current.User.Identity 不会是 WindowsIdentity使用表单身份验证进行模拟

How do you logout when using Windows authentication in ASP.NET like this web.config?

<authentication mode="Windows" />

I've already tried the following unsuccessfully. It redirects, but does not log out the user.

void logoutButton_Click(object sender, EventArgs e) {
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    ViewState.Clear();
    FormsAuthentication.SignOut();
    Response.Redirect("/");
}

Background Info:

I have to use Windows authentication because I need to impersonate the identity using Active Directory to gain access to local files. And I cannot impersonate using Forms authentication because the HttpContext.Current.User.Identity won't be a WindowsIdentity.
Impersonate using Forms Authentication

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

天荒地未老 2024-08-01 10:40:24

使用“Windows”身份验证时,服务器端注销按钮将不起作用。 如果您想要注销按钮,则必须使用“表单”身份验证,或者关闭用户的浏览器。

No server-side logout button will work when using "Windows" authentication. You must use "Forms" authentication if you want a logout button, or close the user's browser.

挽心 2024-08-01 10:40:24

仅适用于 IE 浏览器,如果使用 Windows 身份验证,您可以使用以下 javascript 注销用户。 (注意:关闭浏览器不是必需的,但建议关闭,因为用户可能使用非 IE 浏览器)。

如果用户单击“否”关闭浏览器,则当用户尝试访问网站上需要身份验证的页面时,系统将提示用户输入用户名/密码。

try {
   document.execCommand("ClearAuthenticationCache");
}
catch (e) { }
window.close();

此代码取自 SharePoint 的 Signout.aspx 页面。

For IE browsers only, you can use the following javascript to logout the user if using Windows Authentication. (Note: closing the browser isn't required, but recommended since the user might be using a non-IE browser).

If the user clicks "No" to close the browser, then the user will be prompted for a username/password if they attempt to access a page on the site that requires authentication.

try {
   document.execCommand("ClearAuthenticationCache");
}
catch (e) { }
window.close();

This code was taken from SharePoint's Signout.aspx page.

埋情葬爱 2024-08-01 10:40:24

Windows 身份验证通过传递 Windows 身份验证令牌在 IIS 级别进行工作。 由于身份验证发生在 IIS 级别,因此您实际上无法从应用程序代码中注销。 但是,您的问题似乎有答案这里< /a>. 这是要解决的第二个问题,本质上涉及使用表单身份验证和 LogonUser Windows api。

Windows authentication works at the IIS level by passing your Windows authentication token. Since authentication occurs at the IIS level you cannot actually log out from application code. However, there seems to be an answer to your problem here. It is the second question addressed and essentially involves using Forms Authentication and the LogonUser Windows api.

江挽川 2024-08-01 10:40:24

我有一个带有 Windows 身份验证的 SharePoint 应用程序,我需要在 15 分钟后自动注销。 我混淆了一些代码,这是结果。 它在 IE 中正常工作。

<script type="text/javascript">
var t;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;

function logout() {

    try {
        document.execCommand("ClearAuthenticationCache");
        window.location.href = window.location.protocol.replace(/\:/g, '') + "://" + window.location.host + "/_layouts/customlogin14.aspx";
    }
    catch (e) { }

}

function resetTimer() {
    window.clearTimeout(t);
    t = window.setTimeout(logout, 900000);
} 

将这些代码放入您的主页中,15 分钟空闲时间后您将看到登录页面。
希望这对某人有帮助

I had a SharePoint application with Windows authentication, I needed automatic logout after 15 minutes. I mixed up some codes and here is the result. it works in IE properly.

<script type="text/javascript">
var t;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;

function logout() {

    try {
        document.execCommand("ClearAuthenticationCache");
        window.location.href = window.location.protocol.replace(/\:/g, '') + "://" + window.location.host + "/_layouts/customlogin14.aspx";
    }
    catch (e) { }

}

function resetTimer() {
    window.clearTimeout(t);
    t = window.setTimeout(logout, 900000);
} 

put these codes in your master page, after 15 mins idle time you will see the login page.
hope this help somebody

情定在深秋 2024-08-01 10:40:24

我在 IE 和 Firefox 中都使用 JavaScript 进行了此操作,尽管它会让您退出 IE 中登录的所有内容。 它在 Safari 中可以工作,但 Safari 会发出网络钓鱼警告。 在 Opera 中不起作用。

try {
    if (document.all) {
        document.execCommand("ClearAuthenticationCache");
        window.location = "/";
    } else {
        window.location = "http://logout:[email protected]";
    }
} catch (e) {
    alert("It was not possible to clear your credentials from browser cache. Please, close your browser window to ensure that you are completely logout of system.");
    self.close();
}

I have this working using JavaScript in both IE and Firefox, though it logs you out of everything you're logged into in IE. It sort of works in Safari, but Safari throws up a phishing warning. Doesn't work in Opera.

try {
    if (document.all) {
        document.execCommand("ClearAuthenticationCache");
        window.location = "/";
    } else {
        window.location = "http://logout:[email protected]";
    }
} catch (e) {
    alert("It was not possible to clear your credentials from browser cache. Please, close your browser window to ensure that you are completely logout of system.");
    self.close();
}
诗酒趁年少 2024-08-01 10:40:24

我见过的最佳答案可以在相关的 StackOverFlow 问题中找到:

是否有相当于 IE 的 ClearAuthenticationCache 的浏览器?

使用 HTTP 基本身份验证时注销用户

基本上,您需要使用无效凭据向服务器发送 AJAX 请求,并让服务器接受它们。

The best answers I have seen are found in related StackOverFlow questions:

Is there a browser equivalent to IE's ClearAuthenticationCache?

and

Logging a user out when using HTTP Basic authentication

Basically you need to send a AJAX request to the server with invalid credentials and have the server accept them.

乖乖哒 2024-08-01 10:40:24

遇到了很多麻烦,下面是有效的代码,希望有人发现它有用。

foreach (var cookie in Request.Cookies.Keys)
{
    Response.Cookies.Delete(cookie);
}


await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);


Response.Cookies.Append("EdgeAccessCookie", "", new Microsoft.AspNetCore.Http.CookieOptions()
{
    Path = "/",
    HttpOnly = true,
    SameSite = SameSiteMode.Lax, Expires = DateTime.Now.AddDays(-1)
});


Response.Redirect("https://adfs.[sitename].com/adfs/ls?wa=wsignout1.0");

Had alot of trouble with this, below is the code that works, hopefully someone finds it useful.

foreach (var cookie in Request.Cookies.Keys)
{
    Response.Cookies.Delete(cookie);
}


await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);


Response.Cookies.Append("EdgeAccessCookie", "", new Microsoft.AspNetCore.Http.CookieOptions()
{
    Path = "/",
    HttpOnly = true,
    SameSite = SameSiteMode.Lax, Expires = DateTime.Now.AddDays(-1)
});


Response.Redirect("https://adfs.[sitename].com/adfs/ls?wa=wsignout1.0");
瀟灑尐姊 2024-08-01 10:40:24

如果启用了 Windows 身份验证,您将无法以编程方式注销,我的网页上最终出现了一个注销链接,单击该链接后将显示消息“您已注销,但要完全注销,您需要关闭浏览器”。

另一个变化是我的域控制器 => 组策略=> 用户配置=> 互联网设置 => 本地内联网 => 自定义级别=> 安全=> 用户认证=> “提示输入用户名和密码”选项。

现在,每次打开新的浏览器窗口时,用户都会看到一个 Windows Auth 对话框,从而触发登录。

You cannot log out programically if Windows Auth is enabled, I ended up with having a logout link on my webpage which upon click will present the message "You are logged out, but to completely log out you need to close the browser".

Another change was in my Domain Controller => Group Policy => User Config => Internet Settings => Local Intranet => Custom Level => Security => USer Authentication => "Prompt for user name and Password" option.

The user is now presented with a Windows Auth dialog box every time a new browser window is opened, hence login is triggered.

夏了南城 2024-08-01 10:40:24

我认为你应该使用表单身份验证,但你可以在这样的表单中使用 ldap windows 用户帐户:

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

I think you should use forms auth, but you can use ldap windows user account in forms like this:

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文