Learn the login code of the second MVC instance (model data verification)

Source: Internet
Author: User

1: model layerCode:

 

Namespace mvcapplication5.models
{
Public class userinfo
{
/* User Information Entity */
Public String loginname {Get; set;} // Logon Name
Public String password {Get; set;} // Password
Public String username {Get; set;} // name
}

Public class usermanager // This is equivalent to the BLL and Dal of ordinary three layers.
{
/* User business entity */

/// <Summary>
/// Verify the login name and password
/// </Summary>
Public static bool validate (string loginname, string password)
{
// Simulate user verification here. The actual project needs to read the database user information for verification.
Return "Zhang" == loginname & "123" = password;
}

/// <Summary>
/// Obtain user information based on the login name
/// </Summary>
Public static userinfo getuserbyloginname (string loginname)
{
// Simulate database query here
Return new userinfo ()
{
Loginname = "Zhang ",
Password = "123 ",
Username = "Zhang Xueliang"
};
}
}
}

 

 

 

2: controller code:

 

Public actionresult login ()
{
String loginname = request. Form ["loginname"];
String Password = request. Form ["password"];
If (models. usermanager. Validate (loginname, password ))
{
Session ["currentuser"] = models. usermanager. getuserbyloginname (loginname); // call Method
Return redirect ("/account/success ");
}
Viewdata ["loginname"] = loginname; // This is written to save the user name when an error occurs.
Viewdata ["returnmessage"] = "incorrect user name or password ";
Return view ("Index ");
}

 

 

3: view code:

 

Logon code:

 

<Table border = "0" cellpadding = "0" cellspacing = "0" width = "1000" Height = "768">
<Tr>
<TD align = "center" valign = "Middle" class = "TD">
<Br/>
<% Using (html. beginform ("login", "account") {%>
<Table cellpadding = "5" border = "0" cellspacing = "5" width = "260">
<Tr>
<TD> User name: </TD>
<TD> <% = Html. Textbox ("loginname", viewdata ["loginname"]) %> </ TD>
</Tr>
<Tr>
<TD> password: </TD>
<TD> <% = html. Password ("password") %> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> <input value = "Submit" type = "Submit"/> <br/> <% = viewdata ["returnmessage"] %>
</TD>
</Tr>
</Table>
<%} // Html. endform (); %>
</TD>
</Tr>
</Table>

 

Code after success:

 


<% mvcapplication5.models. userinfo currentuser =
session ["currentuser"] As mvcapplication5.models. userinfo; %>
Hello, welcome: <% = currentuser. username %>
your login name is: <% = currentuser. loginname %>

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.