ASP. net mvc View and Web API basic permission Verification

Source: Internet
Author: User

ASP. net mvc 5.0 has been released for a period of time. It is ready to refactor the original MVC project and record the basic permission verification.

Environment: Windows 7 Professional SP1 + Microsoft Visual Studio 2013 (MVC 5 + Web API 2)

Modify Web. config and add the Forms authentication mode. Add the following configuration to the system. web node:

   

 System.Web.Security;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = , AllowMultiple =     (httpContext ==   (httpContext.User.Identity.IsAuthenticated && = (    (filterContext.HttpContext.Response.StatusCode == (=  RedirectToRouteResult(,             

Create a Controller base class PageBase, inherited from Controller:

 System.Web.Mvc;
 

The controllers of all views are inherited from PageBase and no longer from the Controller.

After inheriting PageBase, All controllers must log on to AllowAnonymous (take AccountController as an example) for the Controller (or Action) that allows anonymous access ):

 System.Web.Mvc;
  ActionResult Login()  =  ActionResult Detail( id)   = 

The development of the page Controller is basically complete. The next step is to log on to the page (~ /Login) Use js to submit login information and post to submit.

After submission, You need to develop the Web API interface.

[MVC Web API Controller]

Similarly, a new ApiAuth is inherited from ActionFilterAttribute:

 System.Web.Security;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = , AllowMultiple =     (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Count > )    cookie = (cookie ==  || cookie.Count < = =  ( perCookie  cookie[ (perCookie.Name === (ticket == =             = 

Create an ApiController base class ApiBase, inherited from ApiController:

 System.Web.Http;
 

The controllers of all APIs are inherited from ApiBase and no longer from ApiController.

After inheriting ApiBase, add AllowAnonymous to the Controller (or Action) that allows anonymous access (take LoginController as an example ):

 System.Web.Security;
   cookie = FormsAuthentication.GetAuthCookie(,  ticket = newTicket =  FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, =  

[Written at the end]

I checked a lot of Methods online and needed time to verify the rationality of each method.

Concerning the security of Web APIs, I personally think that the SSL method is more secure.

In addition, when judging the permissions of Web APIs written on the internet, actionContext. Request. Headers. Authorization is used to determine the permissions, as shown below:

 (actionContext.Request.Headers.Authorization ==  ticket =}

You have not completed the test. Please take a look at it ~~~

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.