The ASP. NET Web API implements identity authentication with the authentication feature

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Net.Http.Headers;4 usingSystem.Security.Principal;5 usingSystem.Text;6 usingSystem.Threading;7 usingSystem.Threading.Tasks;8 usingSystem.Web.Http.Filters;9 usingSystem.Web.Http.Results;Ten  One namespaceWebApi A { -[AttributeUsage (AttributeTargets.Class |AttributeTargets.Method)] -      Public classAuthenticateattribute:filterattribute, Iauthenticationfilter the     { -         Private Static ReadOnlydictionary<string,string>useraccounts; -  -         StaticAuthenticateattribute () +         { -UserAccounts =Newdictionary<string,string>(stringcomparer.ordinalignorecase) +             { A{"Foo","Password"}, at{"Bar","Password"}, -{"Baz","Password"} -             }; -         } -  -          PublicTask Authenticateasync (httpauthenticationcontext context, CancellationToken CancellationToken) in         { -IPrincipal user =NULL; to             varHeadervalue =context. Request.Headers.Authorization; +             if(NULL! = Headervalue && Headervalue.scheme = ="Basic") -             { the                 varCredential =Encoding.Default.GetString (convert.frombase64string (Headervalue.parameter)); *                 varSplit = credential. Split (':'); $                 if(split.) Length = =2)Panax Notoginseng                 { -                     varUserName = split[0]; the                     stringpassword; +                     if(Useraccounts.trygetvalue (UserName, outpassword)) A                     { the                         if(Password = = split[1]) +                         { -                             varIdentity =Newgenericidentity (userName); $user =NewGenericPrincipal (Identity,New string[0]); $                         } -                     } -                 } the             } -Context. Principal =user;Wuyi             returntask.fromresult<Object> (NULL); the         } -  Wu          PublicTask Challengeasync (httpauthenticationchallengecontext context, CancellationToken CancellationToken) -         { About             varuser =context. ActionContext.ControllerContext.RequestContext.Principal; $             if(NULL! = User && user. identity.isauthenticated)returntask.fromresult<Object> (NULL); -             varParameter = $"Realm={context. Request.RequestUri.DnsSafeHost}"; -             varChallenge =NewAuthenticationheadervalue ("Basic", parameter); -Context. Result =NewUnauthorizedresult (New[] {challenge}, context. Request); A             returntask.fromresult<Object> (NULL); +         } the     } -}

The ASP. NET Web API implements identity authentication with the authentication feature

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.