I have code right here:
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>
<%@ 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";
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
string groups = txtUsername.Text;
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if(true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
Response.Cookies.Add(authCookie);
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:
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
{
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;
}
_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?