Get user information from active directory asp.net webforms c#

时间:2016-04-25 09:28:42

标签: c# asp.net webforms active-directory username

I got webforms hosted by IIS

You can log in to this webform site by using active directory username and password

How do I get the other information of this user like first and last name from the active directory this is my current code in the page load after a success log in.

   protected void Page_Load(object sender, EventArgs e)
       {
    FormsIdentity id = (FormsIdentity)User.Identity;
        FormsAuthenticationTicket ticket = id.Ticket;
        Response.Write("<p/>TicketName: " + ticket.Name);  //this ticket.name is the current username logged to the form
        Response.Write("<br/>Cookie Path: " + ticket.CookiePath);
        Response.Write("<br/>Ticket Expiration: " +
                        ticket.Expiration.ToString());
        Response.Write("<br/>Expired: " + ticket.Expired.ToString());
        Response.Write("<br/>Persistent: " + ticket.IsPersistent.ToString());
        Response.Write("<br/>IssueDate: " + ticket.IssueDate.ToString());
        Response.Write("<br/>UserData: " + ticket.UserData);
        Response.Write("<br/>Version: " + ticket.Version.ToString());
        }

Additional info, I use authentication mode="Forms" in the web.config framework 4.0

1 个答案:

答案 0 :(得分:0)

尝试使用PrincipalContext。 例如,要手动检查用户密码,您可以使用以下代码:

using (var context = new PrincipalContext(ContextType.Domain, "MyAdName"))
{
   var isValidUser = context.ValidateCredentials(model.UserName, model.Password);
   if (!isValidUser)
   {
     //Do the staff
   }
}

您可以在context找到您想要的内容。

另外,请查看此帖:get first name last name of logedin windows user?