Giter Club home page Giter Club logo

bearer-authentication's Introduction

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 =)

bearer-authentication's People

Contributors

dnl1 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.