ASP. NET simulated Domain Verification login and password Modification

Source: Internet
Author: User

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 ();
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.