I am working on project for active directory LDAP to reset the users password. i am wondering if there is way to catch if users try to reset previous password. ?
pls help on how to reset the password in Active directory using asp.net app
Here is my code : can you tell where is my mistake
protected void ResetUserPassword(object sender, EventArgs e)
{
var userDn = txtUsername.Text;
var directoryEntry = GetDirectoryEntryByUserName(userDn);
directoryEntry.Invoke("SetPassword", new object[] { "NewPassword123" });
directoryEntry.Properties["LockOutTime"].Value = 0;
directoryEntry.Close();
}
public static DirectoryEntry GetDirectoryEntryByUserName(string userName)
{
var de = GetDirectoryObject(GetDomain());
var deSearch = new DirectorySearcher(de)
{
SearchRoot = de,
Filter = "(&(objectCategory=user)(cn=" + userName + "))"
};
var results = deSearch.FindOne();
// throws Excepton here
// COMexception was unhandled by users code
//Unknown Error (0x8005000)
return results != null ? results.GetDirectoryEntry() : null;
}
private static string GetDomain()
{
string adDomain = WebConfigurationManager.AppSettings["adDomainFull"];
var domain = new StringBuilder();
string[] dcs = adDomain.Split('.');
for (var i = 0; i < dcs.GetUpperBound(0) + 1; i++)
{
domain.Append("DC=" + dcs[i]);
if (i < dcs.GetUpperBound(0))
{
domain.Append(",");
}
}
return domain.ToString();
}
private static DirectoryEntry GetDirectoryObject(string domainReference)
{
string adminUser = WebConfigurationManager.AppSettings["adAdminUser"];
string adminPassword = WebConfigurationManager.AppSettings["adAdminPassword"];
string fullPath = domainReference;
var directoryEntry = new DirectoryEntry(fullPath, adminUser, adminPassword, AuthenticationTypes.Secure);
return directoryEntry;
}
}
}
<appSettings>
<add key="adAdminUser" value="****"/>
<add key="adAdminPassword" value="****"/>
<add key="adDomainFull" value="****"/>
</appSettings>
- this is very urgent
Server Error in '/Password' Application.
--------------------------------------------------------------------------------
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))]
Boracay
Member
142 Points
165 Posts
Active Directory with .NET Programing
Nov 25, 2012 06:32 PM|LINK
Gurus,
I am working on project for active directory LDAP to reset the users password. i am wondering if there is way to catch if users try to reset previous password. ?
pls help on how to reset the password in Active directory using asp.net app
thanks
adeelehsan
All-Star
18287 Points
2740 Posts
Re: Active Directory with .NET Programing
Nov 26, 2012 08:39 AM|LINK
See if the following link help you:
http://code.msdn.microsoft.com/Reset-AD-User-Password-28d99a32
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
Boracay
Member
142 Points
165 Posts
Re: Active Directory with .NET Programing
Dec 01, 2012 09:20 PM|LINK
i tried with that and got error;
Here is my code : can you tell where is my mistake
protected void ResetUserPassword(object sender, EventArgs e) { var userDn = txtUsername.Text; var directoryEntry = GetDirectoryEntryByUserName(userDn); directoryEntry.Invoke("SetPassword", new object[] { "NewPassword123" }); directoryEntry.Properties["LockOutTime"].Value = 0; directoryEntry.Close(); } public static DirectoryEntry GetDirectoryEntryByUserName(string userName) { var de = GetDirectoryObject(GetDomain()); var deSearch = new DirectorySearcher(de) { SearchRoot = de, Filter = "(&(objectCategory=user)(cn=" + userName + "))" }; var results = deSearch.FindOne(); // throws Excepton here // COMexception was unhandled by users code //Unknown Error (0x8005000) return results != null ? results.GetDirectoryEntry() : null; } private static string GetDomain() { string adDomain = WebConfigurationManager.AppSettings["adDomainFull"]; var domain = new StringBuilder(); string[] dcs = adDomain.Split('.'); for (var i = 0; i < dcs.GetUpperBound(0) + 1; i++) { domain.Append("DC=" + dcs[i]); if (i < dcs.GetUpperBound(0)) { domain.Append(","); } } return domain.ToString(); } private static DirectoryEntry GetDirectoryObject(string domainReference) { string adminUser = WebConfigurationManager.AppSettings["adAdminUser"]; string adminPassword = WebConfigurationManager.AppSettings["adAdminPassword"]; string fullPath = domainReference; var directoryEntry = new DirectoryEntry(fullPath, adminUser, adminPassword, AuthenticationTypes.Secure); return directoryEntry; } } } <appSettings> <add key="adAdminUser" value="****"/> <add key="adAdminPassword" value="****"/> <add key="adDomainFull" value="****"/> </appSettings>- this is very urgent
Server Error in '/Password' Application. -------------------------------------------------------------------------------- Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))]smirnov
All-Star
23680 Points
4051 Posts
Re: Active Directory with .NET Programing
Mar 04, 2013 02:57 PM|LINK
The error said that ASP.NET account has no permissions to perform an action. You should call your code under account that has more rights.
Also if account was not found at
var directoryEntry = GetDirectoryEntryByUserName(userDn);
then you should get an error, because there is no check after that if directoryEntry is null or not.