Websecurity Role Authentication

Source: Internet
Author: User
Tags tagname

 Public classMyAuthorizeAttribute:System.Web.Mvc.AuthorizeAttribute { Public New string[] Roles {Get;Set; } protected Override BOOLAuthorizecore (HttpContextBase HttpContext) {BOOLresult =false; if(HttpContext = =NULL)            {                Throw NewArgumentNullException ("HttpContext"); }            if(!httpContext.User.Identity.IsAuthenticated) {}if(Roles! =NULL&&Roles.any (HttpContext.User.IsInRole)) {Result=true; }            if(!result) {HttpContext.Response.StatusCode=403; }            returnresult; }         Public Override voidonauthorization (System.Web.Mvc.AuthorizationContext filtercontext) {stringControllername =FilterContext.ActionDescriptor.ControllerDescriptor.ControllerName; stringActionName =FilterContext.ActionDescriptor.ActionName; stringroles =getactionroles (ActionName, controllername); if(!string. Isnullorwhitespace (roles)) { This. Roles = Roles. Split (New string[] {","}, Stringsplitoptions.removeemptyentries); }            Base.            Onauthorization (Filtercontext); if(FilterContext.HttpContext.Response.StatusCode = =403) {Filtercontext.result=NewRedirectresult ("http://www.baidu.com/"); }        }        Private stringGetactionroles (stringActionstringController) {XElement rootelement= Xelement.load (HttpContext.Current.Server.MapPath ("/") +"Actionroles.xml"); XElement controllerelement= Findelementbyattribute (RootElement,"Controller", Controller); if(Controllerelement! =NULL) {XElement actionelement= Findelementbyattribute (Controllerelement,"Action", action); if(Actionelement! =NULL)                {                    returnActionelement.value; }            }            return ""; }        PrivateXElement Findelementbyattribute (XElement XElement,stringTagName,stringattribute) {            returnXelement.elements (TagName). FirstOrDefault (x = X.attribute ("name").        Value.equals (attribute, stringcomparison.ordinalignorecase)); }    }
<?XML version= "1.0" encoding= "Utf-8"?><Roles>  <Controllername= "Home">    <Actionname= "Index"></Action>    <Actionname= "about">User</Action>    <Actionname= "Contact">Admin</Action>    <Actionname= "Tips">Admin</Action>  </Controller></Roles>

Using Websecurity authentication method, equivalent to the normal way to save the login information in the session.

Websecurity Role Authentication

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.