1. Configure web. config
1: <authorization> <! -- Disable access by unauthorized users -->
2: <deny user="?"></deny>
3: </authorization>
2. Create a Login page
The file name and directory structure of the login page of this "things" must be like this "~ /Account/Login. cshtml "; the key code is as follows:
1: <form method="post">
2: account <input name = "user" type = "text"/>
3: password <input name = "pass" type = "password"/>
4: <input name = "login" type = "submit" value = "Logon" class = "button"/>
5: </form>
Logic code:
1 :@{
2: var returnUrl = Request.QueryString["ReturnUrl"];
3: if(returnUrl.IsEmpty()){
4: returnUrl = "~/Admin/";
5: }
6: if(IsPost){
7: var username = Request.Form["user"];
8: var password = Request.Form["pass"];
9: var nickname = "";
10: var y = FormsAuthentication.HashPasswordForStoringInConfigFile(username+password,"md5");
11: var db = Database.Open("RazorCMS");
12: var sqlSel = "select TOP 1 username,password,nickname from SiteSet";
13: var user = db.QuerySingle(sqlSel);
14: var x = FormsAuthentication.HashPasswordForStoringInConfigFile(user["username"]+user["password"],"md5");
15: if(x==y){
16: nickname = user["nickname"];
17: FormsAuthentication.RedirectFromLoginPage(nickname,true);
18: }
19: }
20: }
3. Create Default. cshtml
Verify that the trend log on is successful:
1: @{
2: var n = Context.User.Identity.Name;
3: }