如何获取当前用户的GUID?

时间:2015-11-05 10:38:13

标签: c# asp.net guid

我想获得当前用户的Guid。 这是我的代码:

System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User;
String username = User.Identity.Name.ToString();
System.IO.File.AppendAllText(Server.MapPath("~/logtext.txt"), "username " +   username + Environment.NewLine);
if (username != "")
{
    if (User.Identity.IsAuthenticated)
    {
         MembershipUser user = Membership.GetUser(User.Identity.Name);
         if (user != null) 
         {
              //get the GUID
              Guid guid = (Guid)user.ProviderUserKey;
              System.IO.File.AppendAllText(Server.MapPath("~/logtext.txt"), "Authenticated: " + User.Identity.Name + Environment.NewLine);
         }
    }
}

尽管用户已通过身份验证,但MembershipUser仍返回null。 我也尝试过使用:

DirectoryEntry entry = new DirectoryEntry();
//entry.Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
entry.Username = User.Identity.Name.ToString();
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(objectclass=user)";

但如果没有entry.Password(我没有),它就无效。

0 个答案:

没有答案