Lucas
Lucas

Reputation: 534

How do I get the user's SID from the server?

I had this code:

public bool UserCheck()
    {
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            try
            {
                using (var user = UserPrincipal.FindByIdentity(context, IdentityType.Sid, WindowsIdentity.GetCurrent().User.Value))
                {
                    if (user != null) return true;
                    else return false;
                }
            }
            catch (System.Runtime.InteropServices.COMException) { return false; }
        }
    }

It would check if the current user exists in the server's AD.

This code was working fine when testing on my machine, but when I published it, the code started to return false, after further debugging, I've found out that

WindowsIdentity.GetCurrent().User.Value

is getting the SERVER'S SID, instead of the User's.

How can I get the User's SID when the code is published?

Upvotes: 0

Views: 1306

Answers (1)

MvdD
MvdD

Reputation: 23504

When you run your site on your development machine, Visual Studio loads the site in IIS Express. IIS Express is running as the logged on user. That's why you were seeing your username when executing:

WindowsIdentity.GetCurrent().User.Value

After publishing the web application, you are probably running it under IIS. You need to configure IIS and ASP.NET to use Integrated Windows Authentication (which either uses Kerberos or NTLM)

See this link for more information or Google for 'ASP.NET and windows authentication'

Upvotes: 2

Related Questions