ASP. net mvc + EF framework + EasyUI implement permission management series (12)-implement user asynchronous login and T4 Template

Source: Internet
Author: User

1. Use Jquery to send asynchronous requests

(1) In the previous blog, we have implemented the verification code and user interface settings. Here we show you the following interface:

(2) Next we first write code on the front-end page to send asynchronous requests. Because we use Jquery to send asynchronous requests, we must introduce the Jquery file. The code for sending asynchronous requests is as follows, I wrote a very detailed comment, and I will not explain anything else here.


1 @ * Reference The JS script of the Jquery file *@
2
3 <script src = "~ /Scripts/jquery-1.7.1.js "> </script>
4
5 <script type = "text/javascript">
6
7 // method triggered when the user refreshes the page
8
9 $ (function (){
10
11 // method for implementing user logon
12
13 LoginUserInfo ();
14
15 // how to modify the verification code when you click
16
17 ClickRemoveChangeCode ();
18
19 });
20
21 // Implement User Login
22
23 function LoginUserInfo (){
24
25 // get the event of clicking the user login button
26
27 $ ("# btnLogin"). click (function (){
28
29 // first obtain the parameter to be passed to the Controller, and the dog causes Json. UName, Pwd, Code
30
31 var postData = {
32
33 UName: $ ("# UName"). val (),
34
35 Pwd: $ ("# Pwd"). val (),
36
37 Code: $ ("# Code"). val ()
38
39 };
40
41 // send an asynchronous request to log on
42
43 $. post ("/Login/CheckUserInfo", postData, function (data ){
44
45 if (data = "OK "){
46
47 window. location. href = "/Home/Index ";
48
49} else {
50
51 alert (data );
52
53 window. location. href = "/Login/Index ";
54
55}
56
57 });
58
59 });
60
61}
62
63 // click to change the verification code again
64
65 function ClickRemoveChangeCode (){
66
67 // first, we get the verification code path
68
69 var code = $ ("# imgCode"). attr ("src ");
70
71 // then assign a value to the path of the Verification Code
72
73 $ ("# imgCode"). attr ("src", code + "1 ");
74
75}
76
77 </script> (3) through the above method, we have sent the information (username, password, and verification code) required for logon to the Controller, in this case, the Controller calls the business logic layer to verify the user name and password.

2. Verify that the user name and password are correct at the business logic layer.

(1) We have already written the Jquery Method for sending the request. At this time, we can see that we want to implement the CheckUserInfo method under the Login controller. What else do we need at this time? Of course, we need to write the business logic to verify whether the user name and password are correct. For this project, the general steps for implementing a business logic are as follows.

(2) Step 1: Generally, we first go to the business logic interface layer (LYZJ. userLimitMVC. IBLL) removes interface constraints. If a user logs on successfully, we return the entity of the user's logon information. The passed parameters are also the Entity objects required by the user. Of course, we can also pass the user name, the password and other strings are determined here. The interface we add to the IUserInfoService class is as follows:


1 public interface IUserInfoService: IBaseService <UserInfo>
2
3 {
4
5 // Add a user login information constraint here
6
7 UserInfo CheckUserInfo (UserInfo userInfo );
8
9} (3) Step 2: After we have added the interface constraints, we will go to the business logic layer (LYZJ. userLimitMVC. BLL) to implement the interface in the UserInfoService class, here we implement the method to verify whether the user name and password are correct.


1 // user verification completed
2
3 public UserInfo CheckUserInfo (UserInfo userInfo)
4
5 {
6
7 // check whether data exists in the Database. If no data exists, a null value is returned.
8
9 return _ DbSession. UserInfoRepository. LoadEntities (u => u. UName = userInfo. UName & u. Pwd = userInfo. Pwd). FirstOrDefault ();
10
11} (4) After the above two steps, we have now implemented a method to determine whether the user name and password are correct, in this case, we can directly call the method completed at the business logic layer in the controller.

3. Verify the verification code in the controller and the user logon process.

(1) When we have completed the correct judgment on the user at the business logic layer, we have to go to the Controller to achieve the final judgment, at this time, we should first judge whether the verification code is correct in the Controller. Secondly, we should judge whether the username and password entered by the user are correct. The Code has been commented out and will not be detailed.


1 // instantiate the UserInfo interface object
2
3 private IBLL. IUserInfoService _ userInfoService = new UserInfoService ();
4
5 // Implement User Login
6
7 public ActionResult Index ()
8
9 {
10
11 return View ();
12
13}
14
15 // determine whether the information entered by the user is correct
16
17 [HttpPost]
18
19 public ActionResult CheckUserInfo (UserInfo userInfo, string Code)
20
21 {
22
23 // first, we get the system verification code.
24
25 string sessionCode = this. TempData ["ValidateCode"] = null
26
27? New Guid (). ToString ()
28
29: this. TempData ["ValidateCode"]. ToString ();
30
31 // then we will remove the verification code to avoid brute force cracking.
32
33 this. TempData ["ValidateCode"] = new Guid ();
34
35 // determine whether the verification code entered by the user is correct
36
37 if (sessionCode! = Code)
38
39 {
40
41 return Content ("Incorrect verification code ");
42
43}
44
45 // call the business logic layer (BLL) to verify whether the user is correct
46
47 var loginUserInfo = _ userInfoService. CheckUserInfo (userInfo );
48
49 if (loginUserInfo! = Null)
50
51 {
52
53 return Content ("OK ");
54
55}
56
57 else
58
59 {
60
61 return Content ("incorrect user name and password ");
62
63}
64
65} (2) Our login is complete. At this time, you can try to log on by yourself. Below I will show some login images.

4. Images of Logon status

(1) through the above introduction, we have achieved the login effect, but here I have not verified whether to enter the login, that is, some text boxes must be entered without verification, however, if you do not enter the content, the user cannot log on, and an error is returned in the background.

  

  

  

 

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.