C# 是否有办法使用Windows身份验证注销并以其他用户身份登录?[ASP.NET Core 3.1 MVC]
我正在使用ASP.NET Core 3.1 MVC制作一个内部使用的intranet web应用程序,该应用程序使用Windows身份验证并处理注销/登录页面。我试图包括一个功能,允许用户注销和登录作为另一个用户 我插入了C# 是否有办法使用Windows身份验证注销并以其他用户身份登录?[ASP.NET Core 3.1 MVC],c#,html,asp.net-mvc,asp.net-core,C#,Html,Asp.net Mvc,Asp.net Core,我正在使用ASP.NET Core 3.1 MVC制作一个内部使用的intranet web应用程序,该应用程序使用Windows身份验证并处理注销/登录页面。我试图包括一个功能,允许用户注销和登录作为另一个用户 我插入了\u Layout.cshtml,但是@User.Identity.Name仍然在网页上显示原始名称,这表明我还没有注销,我仍然可以使用[Authorize]属性访问页面,是否有其他方法注销用户 另外,是否有ASP.NET Core 3.1版本的?当我尝试将其作为函数插入控制器
\u Layout.cshtml
,但是@User.Identity.Name
仍然在网页上显示原始名称,这表明我还没有注销,我仍然可以使用[Authorize]
属性访问页面,是否有其他方法注销用户
另外,是否有ASP.NET Core 3.1版本的?当我尝试将其作为函数插入控制器时,会出现错误,说明HttpCookie
,Request.IsAuthenticated
,Response.Cookies.Set(cookie)
和类似的函数未定义。我是否丢失了一个NuGet包或库?我是否必须注销用户才能使用此“以其他用户身份登录”功能
提前谢谢
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:2322",
"sslPort": 44329
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Loginpagedemo2": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
控制器\LogoutController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace Loginpagedemo2.Controllers
{
public class LogoutController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
视图\注销\\u Layout.cshtml
<!DOCTYPE html>
<script runat="server">
try {
document.execCommand("ClearAuthenticationCache")
}
catch (e) { }
</script>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - CSH Shift Interchange System</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
</div>
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Login as @User.Identity.Name</a>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - CSH Shift Interchange System
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
试一试{
document.execCommand(“ClearAuthenticationCache”)
}
捕获(e){}
@ViewData[“标题”]-CSH轮班交换系统
以@User.Identity.Name登录
@RenderBody()
&抄袭;2020年-CSH换乘系统
@RenderSection(“脚本”,必需:false)
您好,这里有些适合您
控制器
'[HttpPost]'
'public async Task <IActionResult> Logout()
{
await signInManager.SignOutAsync();
foreach (var cookie in HttpContext.Request.Cookies)
{
Response.Cookies.Delete(cookie.Key);
}
ProcessStartInfo pro = new ProcessStartInfo();
pro.FileName = "cmd.exe";
pro.Arguments = "klist purge";
Process proStart = new Process();
proStart.StartInfo = pro;
proStart.Start();
proStart.Close();
return View();
}'
“[HttpPost]”
'公共异步任务注销()
{
等待signInManager.SignOutAsync();
foreach(HttpContext.Request.Cookies中的var cookie)
{
Response.Cookies.Delete(cookie.Key);
}
ProcessStartInfo pro=新的ProcessStartInfo();
pro.FileName=“cmd.exe”;
pro.Arguments=“klist purge”;
proStart流程=新流程();
proStart.StartInfo=pro;
proStart.Start();
proStart.Close();
返回视图();
}'
我的视图注销
"
window.close();
'请添加一些代码添加到控制器和视图据我所知,没有winodws身份验证注销按钮可以注销windows用户。因为windows身份验证令牌是通过浏览器发送的,不会受到您的代码的影响。如果您想使用其他用户登录,您只能使用表单身份验证或其他广告身份验证。@BrandoZhang是否有任何可能的客户端解决方法,让用户在按下按钮或类似操作后以其他用户的身份登录?不,据我所知,IE有一个高级设置,每次都会询问windows帐户和密码,但没有js方法注销windows身份验证。
window.close();
</script>