MVC5 website development practice 2.2, Administrator authentication, mvc52.2

Source: Internet
Author: User

MVC5 website development practice 2.2, Administrator authentication, mvc52.2

The last time the Administrator logs on, the AuthorizeAttribute is used to verify the Administrator logs on. A few issues need to be solved before the verification class is rewritten, and the interface is slightly changed.

Directory

Overview of MVC5 website development practices

MVC5 website development practices 1. Create a project

MVC5 website development practices 2. Background Management

MVC5 website development practice 2.1. Log On As an administrator

I. troubleshooting Home controller error prompt

@ Ted said in the comment that the following error exists in the browser. This is because there are multiple Home controllers in the project, but the system does not know which controller you want to access. Therefore, you need to add a namespace for each route.

Using System. web; using System. web. mvc; namespace Ninesky. website. areas. config {/// <summary> /// administrator authentication /// <remarks> /// create: december 16, 2014 /// </remarks> /// </summary> public class AdminAuthorizeAttribute: AuthorizeAttribute {protected override bool AuthorizeCore (HttpContextBase httpContext) {bool _ pass = false; if (httpContext. session ["Account"]! = Null) _ pass = true; return _ pass;} protected override void HandleUnauthorizedRequest (AuthorizationContext filterContext) {filterContext. Result = new RedirectResult ("~ /Config/Administrator/Login ");}}}

2. Add verification attributes to the Controller

Open the Homecontroller In the config area and add the "AdminAuthorize" attribute to the Controller.

@ Model Ninesky. Website. Areas. Config. Models. LoginViewModel @ {Layout = null;} <! DOCTYPE html>

The modified logon page.

<! DOCTYPE html>

Now you can see the logon page.

======================================

Code: http://pan.baidu.com/s/1ntt9gKT

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.