BearerAuthentication 1.0.1

dotnet add package BearerAuthentication --version 1.0.1
NuGet\Install-Package BearerAuthentication -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BearerAuthentication" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BearerAuthentication --version 1.0.1
#r "nuget: BearerAuthentication, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BearerAuthentication as a Cake Addin
#addin nuget:?package=BearerAuthentication&version=1.0.1

// Install BearerAuthentication as a Cake Tool
#tool nuget:?package=BearerAuthentication&version=1.0.1

bearer-authentication

Contents

NuGet Installation

Install-Package BearerAuthentication

You can find the NuGet package here

About

Bearer authentication is an easy way to implement token authentication on your WebAPI.

Simple to implement.

Implementation

First of all you need to add these three keys on your Web.config to have your own crypto keys, I recommend to change the values.

<add key="BearerAuthentication.Crypto.PasswordHash" value="MYP4SSW0RDH4SH"/>
<add key="BearerAuthentication.Crypto.SaltKey" value="S@LTK3Y"/>
<add key="BearerAuthentication.Crypto.VIKey" value="V1KEY"/>

If you want to add expire time on access token just add

<add key="BearerAuthentication.ExpireMinutes" value="60"/>

Then in your FilterConfig.cs need to add BearerAuthenticationFilter

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
    {
        filters.Add(new BearerAuthenticationFilter());
    }
}

and you alter Global.asax.cs should be like this, adding the Filter, and adding the Application_PostAuthorizeRequest method

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);

    //REGISTERING MY FILTERS
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    FilterConfig.RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);

    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

protected void Application_PostAuthorizeRequest()
{
    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}

To finish you need to implement the step in which the user logs into the system, then it will look like this

BearerToken bearerToken = new BearerToken();
bearerToken.GenerateHeaderToken(user.identifier, user.email);

Then after this step, the access_token will always be updated with each request, so it is necessary that always send the latest access_token

Ok, now you can be happy with Bearer Authentication =)

Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,216 12/29/2017
1.0.0 1,187 12/28/2017