C# 检测使用ASP.NET应用程序登录计算机的用户
我想开发一个ASP.NET应用程序,可以检测登录到Windows域的用户。这些凭据将用于登录ASP.NET应用程序 我该怎么做C# 检测使用ASP.NET应用程序登录计算机的用户,c#,asp.net,active-directory,windows-authentication,C#,Asp.net,Active Directory,Windows Authentication,我想开发一个ASP.NET应用程序,可以检测登录到Windows域的用户。这些凭据将用于登录ASP.NET应用程序 我该怎么做 谢谢 您应该查看active directory成员资格提供程序。它内置于ASP.NET中。这里是我用来对Active Directory进行身份验证的C代码 System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString using System; using System.Directory
谢谢 您应该查看active directory成员资格提供程序。它内置于ASP.NET中。这里是我用来对Active Directory进行身份验证的C代码
System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString
using System;
using System.DirectoryServices;
namespace XYZcompany.Enterprise
{
public class AuthenicationMgr
{
private static readonly int AD_ERR_LOGON_FAIL = -2147023570;
private static readonly string _path = "LDAP://xxx.yyy.ggg";
private static readonly string _domain = "xxx.yyy.ggg";
public static bool IsAuthenticated(string username, string pwd)
{
bool authenticatedFlag = true;
string domainAndUsername = _domain + "\\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
{
authenticatedFlag = false;
}
else
{
authenticatedFlag = true;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
if (ex.ErrorCode == AD_ERR_LOGON_FAIL)
{
authenticatedFlag = false;
}
else
{
throw new ApplicationException("Unable to authenticate user due to system error.", ex);
}
}
return authenticatedFlag;
}
}
}
对于ASP.net,您可能可以使用
HttpContext.Current.User.Identity
如果IIS配置正确(至少没有匿名登录)在IIS中,启用集成Windows身份验证,如果使用以下选项,则在代码中启用:
Request.ServerVariables["LOGON_USER"]
它将返回登录用户的windows用户名,即MYDOMAIN\MYUSERNAME这不适用于ASP.NET应用程序,因为这将返回IIS的凭据,而不是我每天使用的登录用户,它对我和使用网站的所有用户都很好。然后,您可能正在使用身份模拟,这太可怕了,不推荐。我不问这个,但我需要它。非常感谢。如果用户在IIS上匿名登录,我就看不到他的数据。这仅适用于运行IIS的计算机。