C# Asp net核心获取用户Windows用户名
在ASP.net CORE mvc中构建intranet,我需要获取当前用户的Windows用户名进行登录,我不需要使用Windows身份验证自动登录用户,我已经有一个自定义的登录控制器来完成此操作,我只需要他的用户名。C# Asp net核心获取用户Windows用户名,c#,asp.net,authentication,asp.net-core,active-directory,C#,Asp.net,Authentication,Asp.net Core,Active Directory,在ASP.net CORE mvc中构建intranet,我需要获取当前用户的Windows用户名进行登录,我不需要使用Windows身份验证自动登录用户,我已经有一个自定义的登录控制器来完成此操作,我只需要他的用户名。 它在本地运行正常,但在IIS服务器上无法获取用户名: 本地: Environment.UserName => VeronY System.Security.Principal.WindowsIdentity.GetCurrent().Name => Domain\
它在本地运行正常,但在IIS服务器上无法获取用户名:
本地:
Environment.UserName => VeronY
System.Security.Principal.WindowsIdentity.GetCurrent().Name => Domain\VeronY
IIS服务器:
Environment.UserName => Intranet
System.Security.Principal.WindowsIdentity.GetCurrent().Name => APPPOOL\Intranet
有了Windows自动认证,我就可以自动登录,这不是我所需要的。
必须有两种类型的身份验证:带有AD的自动身份验证和带有form manage by Identity Framework的手动身份验证。ASP.net似乎没有授权两种不同类型的连接,因此我让主站点使用表单身份验证,并创建了一个小API:
[Authorize]
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public ActionResult Get()
{
return Json(User.Identity.Name);
}
}
使用Windows身份验证进行配置。这是主网站中的LoginController:
String responseString = "";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://myapiURL");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("api/values").Result;
if (response.IsSuccessStatusCode)
{
responseString = response.Content.ReadAsStringAsync().Result;
responseString = Regex.Unescape(responseString).Replace("\"","");//Because the response is something like \\"Domaine\\\\Username\"\
}
else
{
return View();//server cannot be found or Windows authentication fail => form Login
}
}
String username = "";
String domain = "";
if (responseString != "" && responseString.Contains("\\"))
{
domain = responseString.Split('\\')[0];
username = responseString.Split("\\")[1];
if(domain !="MYDOMAIN")
{
return View();//Not in the correct domain => form Login
}
}
else
{
return View();//Not the correct response => form Login
}
UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), username);
if (null != user)
{
CustomAutomaticLogin(user)//All seems ok, try to log the user with custom login with AD informations
}
else
{
return View()//Not in AD => form login
}
}
在ASP.NET Core中获取Windows用户名 首先更新launchSettings.json文件中的windowsAuthentication和anonymousAuthentication属性。匿名站点访问仍然是可能的 launchSettings.json
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:53321",
"sslPort": 44320
}
}
然后,您可以使用以下代码从控制器获取用户的Windows用户名
HomeController.cs
public IActionResult Index()
{
string userName = HttpContext.User.Identity.Name;
return View(userName);
}
依赖注入
如果要在启动脚本或存储库中访问Windows用户名,则可以依赖项注入HttpContextAccessor,这将允许访问用户对象。更多详情可在此找到
public virtual void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddControllers();
}