Web API authentication Bearer token authentication Bearer token authentication

Source: Internet
Author: User

1. Startup.Auth.cs file

Add Property

public static oauthbearerauthenticationoptions oauthbeareroptions {get; private set;}

Add a static constructor

        <summary>        ///constructor///</summary> static Startup ()        {            oauthbeareroptions = new Oauthbearerauthenticationoptions ();        }    

Method added in Configureauth

            Use an anonymous authentication            app. Useoauthbearerauthentication (oauthbeareroptions);

2. WebApiConfig.cs file

Method register to add the

            Config. Suppressdefaulthostauthentication ();            Config. Filters.add (New Hostauthenticationfilter ("Bearer"));

3. Create an authentication method (Web API)

[HttpPost] public async task<string> Authenticate (string userName, string password) {if (s Tring. IsNullOrEmpty (useraccount) | | String. IsNullOrEmpty (password)) {return string.            Empty; }
User Lookup failed users user = await Usermanager.findasync (userName, password); if (user = = null) {return string. Empty; }//The authentication ticket includes a role or can be replaced with a username var identity = new Claimsidentity (Startup.OAuthBearerOptions.Authentication Type); Identity. Addclaim (New Claim (claimtypes.nameidentifier, user. Id.tostring ())); if (usermanager.supportsuserrole) {ilist<string> roles = await usermanager.getrolesasync ( User. ID). Configureawait (FALSE); foreach (string roleName in roles) {identity. Addclaim (New Claim (Claimtypes.role, RoleName, claimvaluetypes.string)); }} Authenticationticket ticket = new Authenticationticket (Identity, New Authenticationproperties ()) ; var currentutc = Datetime.utcnow; Ticket. PROPERTIES.ISSUEDUTC = CURRENTUTC; Ticket. PRoperties. EXPIRESUTC = Currentutc.add (timespan.fromdays (1)); return value returns Startup.OAuthBearerOptions.AccessTokenFormat.Protect (ticket); }

4. Add a tag for a controller or method that requires authentication

[Authorize (Roles = "Admin")]public class userscontroller:apicontroller{}

Test:

Add a token to the request header in the following format:

Authorization:bearer Boqtj0scgz2gfgz ...

Web API authentication Bearer token authentication Bearer token 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.