Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use bearer Tokens with MVC 6 API?

I am working through some MVC 6 and ASP.NET 5 samples and I am having issues finding any worthy documentation on using bearer tokens to secure API's. I am able to make such samples work with VS 2013, MVC 5 but I am unable to port these over to VS 2015 and MVC 6. Does anyone know of any good samples of implementing bearer tokens in MVC 6 in order to secure API's?

like image 359
Chad Cromer Avatar asked Sep 28 '22 18:09

Chad Cromer


2 Answers

In order to authenticate a request using bearer tokens, you can pull down the Microsoft.AspNet.Security.OAuthBearer package. Then, you can add the OAuthBearerAuthenticationMiddleware middleware to the pipeline by using the UseOAuthBearerAuthentication extension method.

Example:

public void Configure(IApplicationBuilder app)
{

    // ...

    app.UseOAuthBearerAuthentication(options =>
    {
        options.Audience = "Redplace-With-Real-Audience-Info";
        options.Authority = "Redplace-With-Real-Authority-Info";
    });
}

Also, have a look at WebApp-WebAPI-OpenIdConnect-AspNet5 sample.

like image 138
tugberk Avatar answered Oct 05 '22 07:10

tugberk


There is no middleware in Asp.Net Core, which generates bearer token. You can create your own solution or implement some community based approaches like

  • OpenIdConnect
  • OpenIdDict
  • IdentityServer4
like image 27
jasdefer Avatar answered Oct 05 '22 07:10

jasdefer