Click here to Skip to main content
14,303,648 members
   

ASP.NET

 
QuestionRe: How to print from android web browser to Bluetooth thermal printer? Pin
Richard MacCutchan21-Aug-19 22:20
protectorRichard MacCutchan21-Aug-19 22:20 
QuestionRazor Pages Navigation Pin
Mycroft Holmes19-Aug-19 15:26
memberMycroft Holmes19-Aug-19 15:26 
AnswerRe: Razor Pages Navigation Pin
F-ES Sitecore19-Aug-19 23:11
mveF-ES Sitecore19-Aug-19 23:11 
GeneralRe: Razor Pages Navigation Pin
Mycroft Holmes20-Aug-19 14:59
memberMycroft Holmes20-Aug-19 14:59 
AnswerRe: Razor Pages Navigation Pin
Richard Deeming20-Aug-19 1:59
mveRichard Deeming20-Aug-19 1:59 
GeneralRe: Razor Pages Navigation Pin
Mycroft Holmes20-Aug-19 15:01
memberMycroft Holmes20-Aug-19 15:01 
GeneralMessage Closed Pin
29-Aug-19 12:52
membervalycarrolito29-Aug-19 12:52 
QuestionHaving Issues to logging users to active directory through web form Pin
samflex15-Aug-19 4:40
membersamflex15-Aug-19 4:40 
Greetings again,

I have an asp.net web app and have been tasked with validating user log in through Active Directory.

I have been working on this part now for almost two days with no success.

Here is what I have been working on.

I created a table called ADSI_Table

This table has two fields, ParamName and ParamValue:

Here is the complete table info:

ParamName               ParamValue

WindowsDomainServer	pcg.porto.loc
BaseDN	                DC=pcg, DC=porto, DC=loc (Base DN)
UserDN	                OU=InformationSystems (User DN)
GroupName	        CN=ITTESTAPP,  OU=IT-Groups, DC=pcg, DC=porto, DC=loc
AccountFilter	        sAMAccountName


The DB Server is configured to talk to Active Directory Server.

Then I am using the following C# code to query this table:

protected void btnSubmit_Click(object sender, System.EventArgs e)
  {
      if (txtUserName.Text.Trim().Equals("") || txtPassword.Text.Trim().Equals(""))
      {
          Message.Text = "Please Enter UserName/Password...";
          txtPassword.Text = "";
          txtUserName.Text = "";
      }
      else
      {
          GetADSILogin();
      }

  }
  public void GetADSILogin()
  {
      try
      {
          string strServerName = "";
          string strBaseDN = "";
          string strUserDN = "";
          string strGroupName = "";
          string strAccountFilter = "";

          //Port no for LDAP Default is 389
          string strPortNo = "389";

          Boolean blnGroupUser = false;
          //Data source string

          string source = "Data Source=myDBServerName;Initial Catalog=MyDB;user=mysusername;password=myPassword";
          //SQL statement that will be issued

          string select = "SELECT * from ADSI_Table";

          //SQL Connection
          SqlConnection conn = new SqlConnection(source);

          // Open the database connection
          conn.Open();

          // Create the SQL command...
          SqlCommand cmd = new SqlCommand(select, conn);

          //Execute Data reader
          SqlDataReader myReader = cmd.ExecuteReader();

          //Check if any rows return against user/pass
          if (myReader.HasRows)
          {
              while (myReader.Read())
              {
                  //Store the parameter's data in variables
                  string strParameterName = myReader.GetString(0).Trim();
                  string strParameterValue = myReader.GetString(1).Trim();

                  if (strParameterName.ToUpper().Equals("SERVERNAME"))
                      strServerName = strParameterValue;

                  if (strParameterName.ToUpper().Equals("BASEDN"))
                      strBaseDN = strParameterValue;

                  if (strParameterName.ToUpper().Equals("USERDN"))
                      strUserDN = strParameterValue;

                  if (strParameterName.ToUpper().Equals("GROUPNAME"))
                      strGroupName = strParameterValue;

                  if (strParameterName.ToUpper().Equals("ACCOUNTFILTER"))
                      strAccountFilter = strParameterValue;
              }
          }

          //Search for user
          DirectoryEntry deSystem = new DirectoryEntry("LDAP://" + strServerName + "/" + strUserDN + "," + strBaseDN);
          deSystem.AuthenticationType = AuthenticationTypes.Secure;
          deSystem.Username = txtUserName.Text;
          deSystem.Password = txtPassword.Text;

          //Search for account name
          string strSearch = strAccountFilter + "=" + txtUserName.Text;

          DirectorySearcher dsSystem = new DirectorySearcher(deSystem, strSearch);
          //Search subtree of UserDN

          dsSystem.SearchScope = SearchScope.Subtree;

          //Find the user data
          SearchResult srSystem = dsSystem.FindOne();

          //Pick up the user group belong to
          ResultPropertyValueCollection valcol = srSystem.Properties["memberOf"];

          if (valcol.Count > 0)
          {
              foreach (object o in valcol)
              {
                  //check user exist in Group we are searching for
                  if (o.ToString().Equals(strGroupName + "," + strBaseDN))
                  {
                      blnGroupUser = true;
                      break;
                  }
              }
          }

          if (blnGroupUser == true)
              Message.Text = "Login Sucessfull...";
          else
              Message.Text = "User Does Not Belong to Specified ADSI Group";
      }
      catch (Exception ex)
      {
          Message.Text = (ex.Message);
      }
      int i = 0;
      i = i + 1;

      if (i == 5)
      {
          Message.Text = "Login failed for 5 times. Quiting...";
          this.Close();
      }
  }


When a user attempts to login and the log in is successful, the user is redirected to search page although I have not put the redirect code yet as I am busy testing authentication.

If the user does to belong to the specified Active Directory group, the user gets the following message:

User Does Not Belong to Specified AD Group.

So far, that's all I keep getting.

I have run the AD tree and attributes the infrastructure folks who set up the AD and they have confirmed that the entries are correct.

Can someone please tell me what could be wrong with my code or am I missing a key component?

Many thanks in advance.
AnswerRe: Having Issues to logging users to active directory through web form Pin
ZurdoDev15-Aug-19 7:46
professionalZurdoDev15-Aug-19 7:46 
GeneralRe: Having Issues to logging users to active directory through web form Pin
samflex16-Aug-19 3:50
membersamflex16-Aug-19 3:50 
GeneralRe: Having Issues to logging users to active directory through web form Pin
ZurdoDev16-Aug-19 3:53
professionalZurdoDev16-Aug-19 3:53 
AnswerRe: Having Issues to logging users to active directory through web form Pin
Nathan Minier16-Aug-19 8:29
professionalNathan Minier16-Aug-19 8:29 
QuestionHow to Import and Export Excel File in ASP.NET MVC? Pin
Member 1455824213-Aug-19 8:41
memberMember 1455824213-Aug-19 8:41 
AnswerRe: How to Import and Export Excel File in ASP.NET MVC? Pin
ZurdoDev13-Aug-19 10:00
professionalZurdoDev13-Aug-19 10:00 
AnswerRe: How to Import and Export Excel File in ASP.NET MVC? Pin
Vincent Maverick Durano13-Aug-19 11:17
mveVincent Maverick Durano13-Aug-19 11:17 
GeneralRe: How to Import and Export Excel File in ASP.NET MVC? Pin
Member 1455824214-Aug-19 8:49
memberMember 1455824214-Aug-19 8:49 
QuestionSending Email using ASP.NET(MVC) SMTP Pin
Anandkumar Prajapati11-Aug-19 18:54
professionalAnandkumar Prajapati11-Aug-19 18:54 
AnswerRe: Sending Email using ASP.NET(MVC) SMTP Pin
Richard MacCutchan11-Aug-19 21:25
protectorRichard MacCutchan11-Aug-19 21:25 
GeneralRe: Sending Email using ASP.NET(MVC) SMTP Pin
Anandkumar Prajapati20-Aug-19 21:13
professionalAnandkumar Prajapati20-Aug-19 21:13 
GeneralRe: Sending Email using ASP.NET(MVC) SMTP Pin
valycarrolito29-Aug-19 12:53
membervalycarrolito29-Aug-19 12:53 
QuestionWhy can't I Publish web site error warn ? Pin
Member 24584676-Aug-19 22:33
memberMember 24584676-Aug-19 22:33 
AnswerRe: Why can't I Publish web site error warn ? Pin
jkirkerx8-Aug-19 14:15
professionaljkirkerx8-Aug-19 14:15 
AnswerRe: Why can't I Publish web site error warn ? Pin
Member 24584678-Aug-19 18:55
memberMember 24584678-Aug-19 18:55 
GeneralRe: Why can't I Publish web site error warn ? Pin
jkirkerx9-Aug-19 10:36
professionaljkirkerx9-Aug-19 10:36 
GeneralRe: Why can't I Publish web site error warn ? Pin
Member 245846711-Aug-19 18:27
memberMember 245846711-Aug-19 18:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.