简体   繁体   English

使用 c# 和 IIS 10 进行域身份验证

[英]Domain authentication with c# and IIS 10

thank you in advance for any advice given.提前感谢您提供的任何建议。 I have been tasked with creating a web app for our HR department and I have one last issue to sort out, authentication.我的任务是为我们的人力资源部门创建一个 web 应用程序,我还有最后一个问题要解决,身份验证。 We are running IIS 10 on a Windows Server 2019 Data Center server.我们在 Windows Server 2019 数据中心服务器上运行 IIS 10。 Our websites are setup as virtual servers under the DefaultWebSite.我们的网站设置为 DefaultWebSite 下的虚拟服务器。 Under IIS | IIS下| Authentication I have windows & anonymous enabled.身份验证我启用了 windows 和匿名。 This web site will only be accessed internally.这个 web 站点只能在内部访问。

The web site is created using Visual Studio 2019 with c# on the backend. web 站点是使用 Visual Studio 2019 创建的,后端为 c#。 I have tried to use the following to get the userID我尝试使用以下方法获取用户 ID

userName = System.Environment.UserName.ToString();
userName = HttpContext.Current.User.Identity.GetUserId();
userName = User.Identity.GetUserId();
userName = System.Security.Principal.WindowsIdentity.GetCurrent().ToString();

They each return either null or an empty string.它们各自返回 null 或空字符串。 This is the from the web.config file这是来自 web.config 文件

<system.web>
    <authentication mode="Windows" />

I am used to doing this through classic ASP where you can just read the environment variable and get the logged in user, query AD and provide or deny access.我习惯于通过经典 ASP 执行此操作,您可以在其中读取环境变量并获取登录用户、查询 AD 并提供或拒绝访问。

So the ask is either 1) use the current user context and determine access to the web page 2) have the user login to the site and store the login information in SQL.所以问题是 1) 使用当前用户上下文并确定对 web 页面的访问权限 2) 让用户登录该站点并将登录信息存储在 SQL 中。

Thank you for your time感谢您的时间

David大卫

You should turn on impersonation so that you will get the user info:您应该打开模拟,以便获得用户信息:

<configuration>
  <system.web>
    <identity impersonate="true" />
  </system.web>
</configuration>

And also you should turn off Anonymous Authentication.您还应该关闭匿名身份验证。

For more info: Using IIS Authentication with ASP.NET Impersonation有关详细信息: 使用 IIS 身份验证和 ASP.NET 模拟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM