ASP. net mvc uses AuthenticationAttribute to verify logon,

Source: Internet
Author: User

ASP. net mvc uses AuthenticationAttribute to verify logon,

First, add a class AuthenticationAttribute, which inherits AuthorizeAttribute, as follows:

Using System. web; using System. web. mvc; namespace Zhong. web {public class AuthenticationAttribute: AuthorizeAttribute {public override void OnAuthorization (AuthorizationContext filterContext) {// base. onAuthorization (filterContext); // check if (! FilterContext. ActionDescriptor. IsDefined (typeof (AllowAnonymousAttribute), true )&&! FilterContext. actionDescriptor. controllerDescriptor. isDefined (typeof (AllowAnonymousAttribute), true) {// write the logic code to determine whether to log on. // use cookies to determine whether to log on. In order to briefly describe how to use features, the cookie records the username and plaintext password (which must be encrypted in practice). HttpCookie cookie = filterContext. httpContext. request. cookies ["Member"]; if (! (Cookie! = Null & cookie. values ["name"] = "test" & cookie. value ["pass"] = "123") {filterContext. result = new RedirectResult ("/Member/Login ");}}}}}
View Code

Add the Authentication feature to MemberControll. There are three Action methods under the Member controller, one is the homepage Index, the other is the Login on the logon page, and the other is the Login on the Login page in the Post mode, the view code corresponding to the Index is as follows:

@ {ViewBag. Title = "Index" ;}< h2> This is a member center 

The view code for Login is as follows:

@ {ViewBag. title = "Login" ;}< h2> member logon 

 

When you do not log on to Member/Index directly, the system will jump to Login. When you enter the correct username and password to log on, the Index page is displayed.

 

 

 

 

 

The complete MemberController code is as follows:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace Zhong. web. controllers {[Authentication] public class MemberController: Controller {// GET: Member public ActionResult Index () {return View ();} [AllowAnonymous] public ActionResult Login () {return View ();} [AllowAnonymous] [HttpPost] public ActionResult Login (string name, string password) {// to briefly demonstrate the logon judgment, it mainly verifies the role of AuthenticationAttribute, in actual use, you may need to query the database and determine the password to be encrypted. if (name = "test" & password = "123 ") {HttpCookie cookie = new HttpCookie ("Member"); cookie. values ["name"] = "test"; cookie. value ["pass"] = "123"; Response. cookies. add (cookie); return RedirectToAction ("Index") ;}return View ();}}}
View Code

Note: Because the controller uses the Authentication feature, all actions under the request must first pass authorization/logon verification. Login is the logon page, which is generally not logged on, therefore, anonymous access must be allowed, so [AllowAnonymous] is added.

 

 

Another way of writing the above AuthorizationAttribute is to inherit the FilterAttribute and implement the interface IAuthorizationFilter, which is similar to the system's AuthorizeAttribute,

 

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace Zhong. web {public class testattriization: FilterAttribute, IAuthorizationFilter {public void OnAuthorization (AuthorizationContext filterContext) {// throw new NotImplementedException (); // TODO: code to implement logon verification is written here }}}
View Code

 

Related Article

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.