In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to realize Asp.Mvc 2.0 to realize user login and logout function". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
I. Log in
1. Create Model
When logging in, we usually only need to verify the username and password, and whether to save the login COOKIE, so we create a MODEL login class, which only needs to include 3 fields.
/// ///User Login MODEL /// public class Login { /// ///User name /// [DisplayName("username")] public string UserName { get; set; } /// ///Password /// [DisplayName("Password")] public string UserPwd { get; set; } /// ///Save cookies /// [DisplayName("Remember Me")] public bool RememberMe { get; set; }
2. Create View Page
The same login VIEW page, also establish a strong type of page, the reason why I like to establish a strong type of page, because the page and MODEL are associated, in the page can be directly used MODEL. At this point, the view data class of the page should select MvcLogin.Models.Login.
Login user login m.UserName) %> m.UserName)%> m.UserPwd) %> m.UserPwd) %> m.RememberMe) %> m.RememberMe) %>
Html.CheckBoxFor is used to generate a checkbox button
3. Creating a controller
Similarly, we set up two login methods in controller, one to display the page, and one to determine the username and password after clicking the login button.
public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(Models.Login model) { if (new Models.SqlHelper().UserLogin(model)) { //If username exists, go to home page FormsService.SignIn(model.UserName,model.RememberMe); return RedirectToAction("index"); } else { //login failed, go to login page ViewData["msg"] = "Login failed"; return View(model); } }
The second Login method is preceded by an HTTP POST attribute, so it can only accept POST requests.
4. Add a method to judge user name and password in SQLHELPER
/// ///Determine whether the user login is successful /// /// /// public bool UserLogin(Login model) { strUserExist = string.Format(strUserExist, model.UserName, model.UserPwd); SqlConnection con = new SqlConnection(conStr); con.Open(); SqlCommand cmd = new SqlCommand(strUserExist, con); SqlDataAdapter adp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adp.Fill(ds); con.Close(); if (ds != null && ds.Tables[0].Rows.Count > 0) { return true; } return false; }
5. Run login page
At this point we enter the URL in the page, which will take us to the login page,
The effects are as follows:
Click login, login success after turning to the home page, login failure to return to this page, and display prompt information.
When you click Login, it is POST submission mode, and the publicActionResult Login(Models.Login model) method will be called.
The login failure page is as follows
The login page is as follows
II. cancellation
After successful login, turn to the home page, where we will generate a logout connection.
Welcome!
Here's the Html.ActionLink method,
Html.ActionLink is used to generate a link. The first parameter represents the problem of the link, and the second parameter represents actionname, which can be understood as the page of the link.
As you can see from the above code, the logout link points to LoginoFF., That is, the loginoff action method in controller, so we add a loginoff method in controller, after executing loginoff method, we will go to INDEX home page.
/// ///User logout /// /// public ActionResult LoginOff() { FormsService.SignOut(); return RedirectToAction("index"); }"How to implement Asp.Mvc 2.0 to achieve user login and logout function" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.