The last business logic and presentation layer of the architecture are written, you can start the implementation of specific functions, the first implementation of the administrator login, authentication and logoff features.
First, the business Logic Layer 1, the implementation of 256 hashing encryption method.
Ninesky.core "Right Key"-> add-> folder, enter folder name General.
General folder "Right key"-> Add-> class, enter class name security.
References the System.Security.Cryptography namespace (1) and implements the SHA256 static encryption method.
2, the Administrator model class
Ninesky.core "Right Key"-> Add-> class, enter class name administrator.
Add Reference System.ComponentModel.DataAnnotations; Completed class Code
Using System;
Using System.ComponentModel.DataAnnotations; namespace Ninesky.core {///<summary>///Administrator Model///</summary> public class Administrator {[Key] Pu
Blic int Administratorid {get; set;} <summary>///account///</summary> [Required (errormessage = "must enter {0}")] [Stringlength (Minimumleng
th = 4, errormessage = "{0} length {2}-{1} characters")] [Display (Name = "account")] public string Accounts {get; set;}
<summary>///password///</summary> [DataType (Datatype.password)] [Required (errormessage = "must enter {0}")]
[Stringlength (256,errormessage = ' {0} length less than {1} characters ')]
[Display (Name = "password"]] public string Password {get; set;}
<summary>///Login IP///</summary> [Display (Name = "Logon IP")] public string Loginip {get; set;} <summary>///Login Time///</summary> [Display (Name = "logon Time")] public nullable<datetime> Logint
IME {get; set;} <summary>///creation Time///</summary> [Display (Name = creation time)] public DateTime createtime {get; set;}
}
}
3. Data context
Open Ninesky.core->nineskycontext.cs Add Administrators property
The red box is for adding content.
4, Administratormanager Management class
Ninesky.core "Right Key"-> Add-> class, enter class name Administratormanager.
class inherits from Basemanager<administrator>.
Adds a Ninesky.Core.Types reference to the class.
Using Ninesky.Core.Types;
Using System; Namespace Ninesky.core {public class Administratormanager:basemanager<administrator> {///<summary>// /Add///</summary>///<param name= "admin" > Administrator entity </param>///<returns></returns> Public
Override Response Add (Administrator admin) {Response _resp = new Response (); If Hasaccounts (admin. Accounts)) {_resp.
Code = 0; _resp.
Message = "Account already exists"; else _resp = base.
ADD (admin);
return _RESP; ///<summary>///Modify password///</summary>///<param name= "Administratorid" > Primary key </param>/// ;p Aram name= "password" > New password "ciphertext" </param>///<returns></returns> public Response ChangePassword (
int Administratorid, string password) {Response _resp = new Response ();
var _admin = find (Administratorid); if (_admin = = null) {_resp.
Code = 0; _resp.
message = "Administrator of this primary key does not exist"; else {_admin.
Password = Password; _resp = Update (_admin);
return _RESP; ///<summary>///Delete///</summary>///<param name= "Administratorid" > Primary key </param>///<r
Eturns></returns> public override Response Delete (int administratorid) {Response _resp = new Response (); if (Count () = 1) {_resp.
Code = 0; _resp.
Message = "Cannot delete a unique administrator account"; else _resp = base.
Delete (Administratorid);
return _RESP; ///<summary>///Lookup///</summary>///<param name= "Accounts" > account </param>///<returns& Gt;</returns> Public Administrator Find (string accounts) {return base.
Repository.find (a => a.accounts = = Accounts); ///<summary>///account exists///</summary>///<param name= "Accounts" > account </param>///<retu Rns></returns> public bool Hasaccounts (string accounts) {return base. Repository.iscontains (a => a.accounts.toupper () = = Accounts.
ToUpper ()); ///<summary>///Update login information///</summary>
<param name= "Administratorid" > Primary key </param>///<param name= "IP" >ip address </param>///<param n Ame= "Time" > Times </param>///<returns></returns> public Response upadatelogininfo (int
Administratorid, String IP, DateTime time) {Response _resp = new Response ();
var _admin = find (Administratorid); if (_admin = = null) {_resp.
Code = 0; _resp.
message = "Administrator of this primary key does not exist"; else {_admin.
Loginip = IP; _admin.
Logintime = time;
_resp = Update (_admin);
return _RESP; ///<summary>///validation///</summary>///<param name= "Accounts" > account </param>///<param na Me= "password" > password "ciphertext" </param>///<returns>code:1-succeeded; 2-account number does not exist; 3-Password error </returns> public Response
Verify (string accounts, string password) {Response _resp = new Response (); var _admin = base.
Repository.find (a => a.accounts = = Accounts); if (_admin = = null) {_resp.
Code = 2; _resp. message = "accounts for:" + accountS + "" "The administrator does not exist"; else if (_admin. Password = = Password) {_resp.
Code = 1; _resp.
Message = "Validate Pass"; else {_resp.
Code = 3; _resp.
message = "Account password error";
return _RESP;
}
}
}
Second, the display layer implementation
First, add CSS.
Ninesky.web->content "Right Key"-> add-> Sample table, enter name Stylecontrol.
Open Ninesky.web->app_start->bundleconfig.cs.
Add the code in the Red box. STYLECONTROL.CSS concrete contents were omitted here.
Second, add a reference to the Ninesky.core.
Ninesky.web-> refers to the "right key"-> add a reference ... Select the project-> solution->ninesky.core in the reference manager.
After the two items are processed, the details are continued:
1, the Administrator authentication class Adminauthorizeattribute
Adminauthorizeattribute inherits from Authorizeattribute, overrides Authorizecore method, through session["Adminid"] To determine if an administrator is logged in, rewrite the Handleunauthorizedrequest method to handle page jumps when not logged in.
Using System.Web;
Using SYSTEM.WEB.MVC;
Namespace Ninesky.Web.Areas.Control
{
///<summary>
///Administrator Authentication class
///</summary>
public class Adminauthorizeattribute:authorizeattribute
{
///<summary>
///Override custom authorization checks
/// </summary>
///<returns></returns>
protected override bool Authorizecore ( HttpContextBase HttpContext)
{
if (httpcontext.session["adminid"] = = null) return false;
else return true;
}
<summary>
///overrides unauthorized HTTP request processing
///</summary>
protected override void Handleunauthorizedrequest (AuthorizationContext filtercontext)
{
Filtercontext.result = new Redirectresult ("~/control/admin/login");}}
Ninesky.web->areas->control "Right Key"-> add-> class, input controller name HomeController.
Add [adminauthorize] for HomeController
2, the Administrator controller Ninesky.web->areas->control->controllers "right key"-> add-> Controller. Select MVC5 Controller-NULL, enter controller name admin.
Reference the Ninesky.core, Ninesky.Core.General, and Ninesky.Web.Areas.Control.Models Namespaces in the controller.
Add Private variable Administratormanager adminmanager = new Administratormanager ();
Add [adminauthorize] for Admincontroller
3.1 Admin Login
3.1.1 Login View Model
Ninesky.web->areas->control->models "Right Key"-> Add-> class, enter class name Loginviewmodel.
Namespace Ninesky.Web.Areas.Control.Models
{
///<summary>
///login model
///</summary> Public
class Loginviewmodel
{
///<summary>
///account
///</summary>
[Required (errormessage = ' must enter {0} ']]
[ Stringlength (minimumlength = 4, errormessage = "{0} length is {2}-{1} characters")]
[Display (Name = account number)]] public
string Accounts {get; set;}
<summary>
///password
///</summary>
[DataType (Datatype.password)]
[Required ( ErrorMessage = "must enter {0}")]
[Stringlength (20,minimumlength =6, errormessage = "{0} length {2}-{1} characters")]
[Display ( Name = "password")] public
string Password {get; set;}}}
3.1.2 Login Method
Add the login () method to the Admincontroller
<summary>
///Login
///</summary>
///<returns></returns>
[ AllowAnonymous] public
actionresult Login ()
{return
View ();
}
3.1.3 Login View
Click Right key on the Login () method to add a view->
Template selection Create, model class selection Loginviewmodel, option to select the Reference Script library. After completion code
@model Ninesky.Web.Areas.Control.Models.LoginViewModel @{Layout = null;} <! DOCTYPE html>
How to add a login in Admincontroller public actionresult Login (Loginviewmodel loginviewmodel)
[AllowAnonymous]
[Validateantiforgerytoken]
[HttpPost]
Public ActionResult Login (Loginviewmodel loginviewmodel)
{
if (modelstate.isvalid)
{
string _ PASSOWRD = security.sha256 (Loginviewmodel.password);
var _response = adminmanager.verify (loginviewmodel.accounts, _PASSOWRD);
if (_response. Code = = 1)
{
var _admin = Adminmanager.find (loginviewmodel.accounts);
Session.add ("Adminid", _admin. Administratorid);
Session.add ("Accounts", _admin. Accounts);
_admin. Logintime = DateTime.Now;
_admin. Loginip = request.userhostaddress;
Adminmanager.update (_admin);
Return redirecttoaction ("Index", "Home");
}
else if (_response. Code = = 2) modelstate.addmodelerror ("Accounts", _response. message);
else if (_response. Code = = 3) modelstate.addmodelerror ("Password", _response. message);
else Modelstate.addmodelerror ("", _response). message);
}
Return View (Loginviewmodel);
}
4. Cancellation
Add a logoff processing method to Admincontroller public actionresult Logout ()
<summary>
///logoff
///</summary>
///<returns></returns>
Public ActionResult Logout ()
{
session.clear ();
Return redirecttoaction ("Login");
The completion can be tested by F5.
Login interface, enter account mzwhj password 123456, login successfully.
Login successful interface.
=====================================================
Code See: Https://ninesky.codeplex.com/SourceControl/latest
Code Download: https://ninesky.codeplex.com Click Source code click Download to download the original file.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.