1

I want to get the Guid of the current user. Here is my code:

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);
         }
    }
}

The MembershipUser return null although the user is authenticated. I also tried to use:

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)";

But it does not work without entry.Password (I don't have it).

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
o_s
  • 11
  • 4

0 Answers0