Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have code right here:
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>

C#
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>

<html>
<head>
    <title>Login</title>
</head>
  <body  align="center">
    <form id="Login" method="post" runat="server">
    <table cellpadding="1" cellspacing="1" style="background-color: #E0E0E0; border: 1px solid #727272"  align="center">
      <h1>PT. Packet Systems Indonesia <br/> Login Form</h1>
      <p>Please enter your account in this form!</p>
      <tr>
        <td>
            <asp:Label ID="Label1" Runat=server >Domain:</asp:Label>
      
        </td>
        <td>
            <asp:TextBox ID="txtDomain" Runat=server ></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>
            <asp:Label ID="Label2" Runat=server >Username:</asp:Label>
        </td>
        <td>
            <asp:TextBox ID=txtUsername Runat=server ></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>
            <asp:Label ID="Label3" Runat=server >Password:</asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtPassword" Runat=server TextMode=Password></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td colspan="2">
            <asp:CheckBox ID=chkPersist Runat=server Text="Persist Cookie" />
        </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
            <asp:Button ID="btnLogin" Runat=server Text="Login" OnClick="Login_Click"></asp:Button>
        </td>
      </tr>
      <tr>
        <td colspan="2">
            <asp:Label ID="errorLabel" Runat=server ForeColor=#ff3300></asp:Label>
        </td>
      </tr>
    </table>
    </form>
  </body>
</html>
<script runat=server>
void Login_Click(object sender, EventArgs e)
{
  string adPath = "LDAP://erpad-tbs-1.packet-systems.com"; //Path to your LDAP directory server
  LdapAuthentication adAuth = new LdapAuthentication(adPath);
  try
  {
    if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
    {
      //string groups = adAuth.GetGroups();
      string groups = txtUsername.Text;
      //Create the ticket, and add the groups.
      bool isCookiePersistent = chkPersist.Checked;
      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, 
                txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

      //Encrypt the ticket.
      string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

      //Create a cookie, and then add the encrypted ticket to the cookie as data.
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

      if(true == isCookiePersistent)
      authCookie.Expires = authTicket.Expiration;

      //Add the cookie to the outgoing cookies collection.
      Response.Cookies.Add(authCookie);

      //You can redirect now.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
    }
    else
    {
      errorLabel.Text = "Authentication did not succeed. Check user name and password.";
    }
  }
  catch(Exception ex)
  {
    errorLabel.Text = "Error authenticating. " + ex.Message;
  }
}
</script>

in that code i have domain, username and password TextBox. And they will be authenticated in ldap.

here's my ldapauthentication.cs code:
C#
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
 
namespace FormsAuth
{
    public class LdapAuthentication
    {
        private string _path;
        private string _filterAttribute;
        
        public LdapAuthentication(string path)
        {
            _path = path;
        }
 
        public bool IsAuthenticated(string domain, string username, string pwd)
        {
            string _domain = domain;
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
 
            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = entry.NativeObject;
 
                DirectorySearcher search = new DirectorySearcher(entry);
 
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
 
                if (null == result)
                {
                    return false;
                }
 
                //Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute = (string)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }
 
            return true;
        }
 
        public string GetGroups()
        {
            DirectorySearcher search = new DirectorySearcher(_path);
            search.Filter = "(cn=" + _filterAttribute + ")";
            search.PropertiesToLoad.Add("memberOf");
            StringBuilder groupNames = new StringBuilder();
 
            try
            {
                SearchResult result = search.FindOne();
                int propertyCount = result.Properties["memberOf"].Count;
                string dn;
                int equalsIndex, commaIndex;
 
                for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                {
                    dn = (string)result.Properties["memberOf"][propertyCounter];
                    equalsIndex = dn.IndexOf("=", 1);
                    commaIndex = dn.IndexOf(",", 1);
                    if (-1 == equalsIndex)
                    {
                        return null;
                    }
                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                    groupNames.Append("|");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error obtaining group names. " + ex.Message);
            }
            return groupNames.ToString();
        }
 
        
    }
}

how can i use the value of logon form in to change password form?
i want to change password.

What I have tried:

I tried to make new parameters, but i still confuse.
I just want to take value of old password and change it to new password.
can somebody help me?
Posted
Updated 21-Jun-16 20:51pm
Comments
Sinisa Hajnal 21-Jun-16 3:48am    
Simple answer is you can't :) Original textbox is the control on the login page and as such is not available on any other page.

What you CAN do is a) ask for the user to type it OR b) put username into session, URL or use some other method of transferring data between pages or c) based on login token (however you keep the user logged in) query the database for user details.

You could store the old password in Session. Other option is to use Profile Provider or perhaps creating a custom class that holds the information you need after logon. You can also look into ASP.NET Identity to add custom claims. But I would prefer to let the user type their old password for changing new password for security reasons.
 
Share this answer
 
Comments
Yosua Michael 21-Jun-16 20:30pm    
yes i do, the user will be type their old password.
now the problem is, i can't use the old password.
i guess that my code didn't connect to ldap.
i still confuse, what fungsion code to connect my changepassword to ldap, so i can take the old password.
thank you
Vincent Maverick Durano 22-Jun-16 6:33am    
I would suggest you to debug your code, set a break point and step into it so you'll figure out what went wrong. Also, you might want to look at this article: http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#40a
i suggest you that you must ask old password to change password
other wise if you just want to change it with logon so you need to use Section
basically section is pass data from one form to other and you can call session any where

To add value in session variable

C#
Session.Add("Programe",  ddlProgamme.SelectedValue);

"Program" is a name of session variable
it's data type is by default string

To Get value from session variable
C#
this.program = (string)(Session["Programe"]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900