5

We have an internal facing (C#/MVC/IIS7) application for which the requirement is for users to not have to enter credentials, but we need the network/Windows username to identify who the current user is.

Is there any way to accomplish this? Thanks!

MikeP
  • 71
  • 1
  • 2

2 Answers2

2

Here's some code that deals with that kind of thing:

public class WindowsIdentityHelper
{
    public WindowsPrincipal GetWindowsPrincipal()
    {
        //Get Current AppDomain
        AppDomain myDomain = Thread.GetDomain();
        myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        return (WindowsPrincipal)Thread.CurrentPrincipal;
    }

    public bool IsUserBelongsToWindowsAdministratorGroup()
    {
        WindowsPrincipal myPrincipal = GetWindowsPrincipal();

        if (myPrincipal.IsInRole("Administrators"))
            return true;

        if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            return true;
        else
            return false;
    }

    public string GetFullDomainLoginUserName()
    {
        WindowsPrincipal myPrincipal = GetWindowsPrincipal();
        return myPrincipal.Identity.Name.ToString();
    }

    public string GetLoginUserName()
    {
        string authenticatedUser = string.Empty;
        string userName = GetFullDomainLoginUserName();
        if (userName.Contains("\\"))
            authenticatedUser = userName.Split('\\')[1];
        else
            authenticatedUser = userName;
        return authenticatedUser;
    }
}
Jeff Prince
  • 658
  • 6
  • 13
2

This works for me (ASP.NET C#):

string username = Request.LogonUserIdentity.Name;
SBF
  • 1,252
  • 3
  • 12
  • 21