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 %>