ExampleCodeFor ASP. NET MVCProgram, For reference only!
Namespace to be referenced:
UsingSystem. directoryservices;
Login. cshtml code:
@{
Viewbag. Title = "simulated Domain Verification ";
Layout = "~ /Views/shared/_ layout. cshtml ";
}
< H2 > Simulated Domain Verification </ H2 >
@ Using (html. beginform ()){
< P > Username: @ html. Textbox ("username ") </ P >
< P > Old password: @ html. Password ("oldpassword ") </ P >
< P > New Password: @ html. Password ("newpassword ") </ P >
< P > < Input Type = "Submit" Value = "Login" /> </ P >
}
@ Viewdata ["MSG"]
CS code:
[Acceptverbs (httpverbs. Post)]
[Validateinput ( False )]
Public Actionresult login ( String Username, String Oldpassword, String Newpassword)
{
Directoryentry ad = New Directoryentry ();
Ad. Path = String . Format ( " LDAP: // {0} " , IPaddress );
Ad. Username = " Domain " + @" \ " + Username;
Ad. Password = Oldpassword;
Ad. authenticationtype = Authenticationtypes. secure;
Try
{
Directorysearcher searcher = New Directorysearcher (AD );
Searcher. Filter = String. Format ( " (& (Objectclass = user) (samaccountname = {0 })) " , Username );
System. directoryservices. searchresult result = Searcher. findone ();
If (Result ! = Null )
{
Directoryentry userentry = Result. getdirectoryentry ();
If (Userentry ! = Null )
{
Try
{
Userentry. Invoke ( " Changepassword " , New Object [] {oldpassword, newpassword });
Userentry. commitchanges ();
Userentry. Close ();
Viewdata [ " MSG " ] = " Password Changed! " ;
}
Catch (Exception ex)
{
Viewdata [ " MSG " ] = " Operation failed. Error cause: " + Ex. tostring ();
}
}
}
Else
{
Viewdata [ " MSG " ] = " Operation failed. Unknown account or Password error! " ;
}
Ad. Close ();
}
Catch (Exception ex)
{
Viewdata [ " MSG " ] = " Operation failed. Error cause: " + Ex. tostring ();
}
Return View ();
}