Giter VIP home page Giter VIP logo

webapitokenauthbootstrap's Introduction

Visit the documented Wiki for detailed examples and information.

Features

Token Based User Authentication User Property inside the TokenAuthApiController (Id, Username, Role, LastAccess).

Token Based User Authorization TokenAuthorizeAttribute with AccessLevel - Public, User, Admin or Anonymous.

Built-in Functionality Login(), Logoff(), Ok(), Error(), Unauthorized() Responses with various overloads.

Shared Dynamic Object Betwen Client and Server UserData Property inside the TokenAuthApiController (Up to size of 4 KB storage). Great for caching data in the client side, especially for thin back-end applications like Single Page Applications.

Getting Started

Install Package from Nuget console: Install-Package WebApiTokenAuth

Now, your controllers should inherit from TokenAuthApiController instead of the default ApiController, so you can access its extensions:

  • Access to the properties UserMetadata User and dynamic UserData.
  • Access following functions: Login(), Logout(), Error() and Unauthorize().

Note

In order to use the UserData dynamic object that enables client side caching using cookies, add the following line to the Application_Start() function inside the Global.asax file: GlobalConfiguration.Configuration.Filters.Add(new UserDataModificationActionFilter());

Code Sample

Here I demonstrate the simplicity of using the WebApiTokenAuth package with Register, Login and Logout functionality.

/// <summary>
/// Handles all the account related actions - user registration, login and logout.
/// </summary>
public class AuthController : TokenAuthApiController
{
    // GET api/auth/login
    [ActionName("login")]
    [TokenAuthentication(AccessLevel.Anonymous)]
    public HttpResponseMessage PostLogin([FromBody]LoginViewModel user)
    {
        // Input validaiton.
        if (user == null || user.Username == null || user.Password == null)
        {
            return Error("Please enter username and password.");
        }

        // Retrieve the user data from the Data access layer.
        IDal dal = new ProGamersDal();
        var currentUser = dal.GetUser(user.Username, user.Password);

        // If not match found - return error.
        if (currentUser == null)
        {
            return Error("Bad username or password.");
        }

        // Cache username and user role at the client side as cookie - accessible by javascript at the client side as json object.
        // Note this data is not secured since the user can access the cookie. Don't store any sensitive information there.
        // In case you save login data in the client side as I did, Server-side validation is a MUST.
        UserData.username = "Dgandalf";
        UserData.role = (int) UserRole.User;

        // Creates an access token for this user, stores it in the configured TokenStorage (By default use in-memory storage).
        // You can set different TokenStorage at TokenAuthenticationConfiguration.TokenStorage in your Application_Start 
        // function inside theg lobal.asax file. Additionally, sends cookie with the generated access token to the user.
        return Login(1, "Dgandalf", UserRole.User);

    }

    // POST api/auth/logout
    [ActionName("logout")]
    [TokenAuthentication(AccessLevel.User)]
    public HttpResponseMessage PostLogout()
    {
        // Deletes the token and user-data cookies with the generated access token to the user.
        return Logout();
    }

    // POST api/auth/register
    [ActionName("register")]
    [TokenAuthentication(AccessLevel.Anonymous)]
    public HttpResponseMessage PostRegister(User user)
    {
        // Handle registration data here.

        // Returns OK response. You can also use Login() function instead, so the user will be logged in 
        // automaticly after a successful registration.
        return Ok();
    }
}

Support or Contact

Found a bug or an unintentional behaviour? add it to the 'Issues' section and I will take care of it as soon as possible.

For feature requests or further questions related to WebAPI Token Auth Bootstrap contact me at [email protected]

webapitokenauthbootstrap's People

Contributors

aviranco avatar

Watchers

James Cloos 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.