Search


Tuesday, January 6, 2009

How to get the full name of the user who is logged in to a asp.net website using integrated authentication

to get the full name of the user who is logged in to a asp.net website using integrated authentication need to add reference to System.DirectoryServices.dll

    private string GetFullName(string strLogin)

    {

        string str = "";

        // Parse the string to check if domain name is present.

        int idx = strLogin.IndexOf('\\');

        if (idx == -1)

        {

            idx = strLogin.IndexOf('@');

        }

 

        string strDomain;

        string strName;

 

        if (idx != -1)

        {

            strDomain = strLogin.Substring(0, idx);

            strName = strLogin.Substring(idx + 1);

        }

        else

        {

            strDomain = Environment.MachineName;

            strName = strLogin;

        }

 

        DirectoryEntry obDirEntry = null;

        try

        {

            obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);

            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;

            object obVal = coll["FullName"].Value;

            str = obVal.ToString();

        }

        catch (Exception ex)

        {

            str = "";

            Trace.Write(ex.Message);

        }

        return str;

    }            

No comments:

Post a Comment