C# 无法获取访问网站的用户的用户名和角色
我试图获取当前正在访问web应用程序的用户的名称和角色,但我编写的代码获取服务器用户名 你能回顾一下我写的以下代码并告诉我这个问题的解决方案吗C# 无法获取访问网站的用户的用户名和角色,c#,asp.net,C#,Asp.net,我试图获取当前正在访问web应用程序的用户的名称和角色,但我编写的代码获取服务器用户名 你能回顾一下我写的以下代码并告诉我这个问题的解决方案吗 string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; string[] stringSeparators = new string[] { "\\" }; string[] uname = userName.Split(stringSeparators
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string[] stringSeparators = new string[] { "\\" };
string[] uname = userName.Split(stringSeparators, StringSplitOptions.None);
userName = uname[1];
List<string> userRoles = new List<string>();
userRoles = getUserRole(userName);
public List<string> getUserRole(string userName)
{
List<string> userNestedMembership = new List<string>();
DirectoryEntry domainConnection = new DirectoryEntry(); // Use this to query the default domain
DirectorySearcher samSearcher = new DirectorySearcher();
samSearcher.SearchRoot = domainConnection;
samSearcher.Filter = "(samAccountName=" + userName + ")";
samSearcher.PropertiesToLoad.Add("displayName");
SearchResult samResult = samSearcher.FindOne();
if (samResult != null)
{
DirectoryEntry theUser = samResult.GetDirectoryEntry();
theUser.RefreshCache(new string[] { "tokenGroups" });
foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
{
System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);
DirectorySearcher sidSearcher = new DirectorySearcher();
sidSearcher.SearchRoot = domainConnection;
sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";
sidSearcher.PropertiesToLoad.Add("distinguishedName");
SearchResult sidResult = sidSearcher.FindOne();
if (sidResult != null)
{
string role = (string)sidResult.Properties["distinguishedName"][0];
role = role.Substring(3, role.Length - 3);
string[] roles = role.Split(',');
userNestedMembership.Add(roles[0]);
}
}
}
}
string userName=System.Security.Principal.WindowsIdentity.GetCurrent().Name;
字符串[]字符串分隔符=新字符串[]{“\\”};
string[]uname=userName.Split(stringseparator,StringSplitOptions.None);
用户名=uname[1];
List userRoles=new List();
userRoles=getUserRole(用户名);
公共列表getUserRole(字符串用户名)
{
List userNestedMembership=new List();
DirectoryEntry domainConnection=new DirectoryEntry();//使用此选项查询默认域
DirectorySearcher samSearcher=新的DirectorySearcher();
samSearcher.SearchRoot=域连接;
samSearcher.Filter=“(samAccountName=“+userName+””);
samSearcher.PropertiesToLoad.Add(“displayName”);
SearchResult samResult=samSearcher.FindOne();
if(samResult!=null)
{
DirectoryEntry theUser=samResult.GetDirectoryEntry();
RefreshCache(新字符串[]{“tokenGroups”});
foreach(user.Properties[“令牌组”]中的字节[]结果字节)
{
System.Security.Principal.SecurityIdentifier myId=新的System.Security.Principal.SecurityIdentifier(结果字节,0);
DirectorySearcher sidSearcher=新的DirectorySearcher();
sidSearcher.SearchRoot=域连接;
sidSearcher.Filter=“(objectSid=“+mySID.Value+””);
sidSearcher.PropertiesToLoad.Add(“DiscrimitedName”);
SearchResult sidResult=sidSearcher.FindOne();
if(sidResult!=null)
{
字符串角色=(字符串)sidResult.Properties[“DifferentizedName”][0];
role=role.Substring(3,role.Length-3);
string[]roles=role.Split(',');
userNestedMembership.Add(角色[0]);
}
}
}
}
我没有在web配置中做任何更改
userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
这将获得wndows用户名,很可能是apppool名称
您是否已将web配置和IIS设置为知道您要使用windows身份验证
(如果不是,请尝试使用HTTP上下文
HttpContext.Current.User.Identity.Name
希望这对您有所帮助,或者至少给您一个正确的方向问题可能不在于代码,而在于环境配置。要使
System.Security.Principal.WindowsIdentity.GetCurrent()符合特定的要求.Name
根据需要工作,因为它可以在服务器端获取用户。描述如何使IIS在用户帐户下工作(使用Windows身份验证).用户如何通过网站进行身份验证?这是一个内部网站,将由特定部门的用户使用。我正在尝试获取访问该网站的人的用户名、显示名和用户角色。我尝试了多种方法,但每次都会给我应用程序池用户的名称。我尝试了,但仍然没有成功s正在给我应用程序池标识。你的web配置(身份验证部分)是什么样子的?我尝试过,但仍然给了我应用程序池标识。