Code Monkey home page Code Monkey logo

worlddomination.web.mvc.requestheaderauthorizeattribute's Introduction

Archived - 10/04/2021

--

Request Header Authorize Attribute for an ASP.NET MVC application

This library gives the option for an ASP.NET MVC application to accept authorizing credentials via a custom request Http Header. A good example of this is an OAuth Token, Basic Authentication or some custom Api key.

So how does the Token actually verify that it's legit? Well .. that's the only thing you need to implement. I have no idea how to talk to your database or whatever, so you need to define that.

Required Arguments

Authorization : a class that impliments ICustomAuthorization. (I explain why, below).

Optional Arguments

RequireSsl: require the request to be secure? Default is false, and LocalHost is ignored.

Header: what header key should we check for? Default is Authorization.

Notes

The library includes an InMemory CustomAuthorization class .. which is just an in memory Dictionary .. pretending it's a database. This can be used for testing or R&D purposes.

So lets see how we can do this :)

Code speaks.

[RequestHeaderAuthorize(Authorization = new InMemoryCustomAuthorization("aaa", "Pew Pew"))]
public ActionResult Post(PostInputModel postInputModel)
{ ... }

Here, i'm saying that this ActionMethod requires some Token to be provided in the header. Header key is defaulted to Authorization. Secondly, I've hardcoded the list of Tokens <-> User Names. (Remember, I said this is a contrite example). So the following header will Authorize the request: Authorization: aaa

Curl: curl -H "Authorization: aaa" http://api.PewPew.com

Lets try another example, this time with a real ICustomAuthorization.cs class ...

public class MyDatabaseRequestHeaderAuthorization : ICustomAuthorization.cs
{
    private readonly IDatabase _myDatabase;

    public MyDatabaseRequestHeaderAuthorization (IDatabase myDatabase)
    {
        _myDatabase = myDatabase;
    }

    #region ICustomAuthorization Members

    public string AuthenticationType
    {
        get { return "MyDatabaseRequestHeaderAuthorization "; }
    }

    public bool TryAuthorize(string token, out IPrincipal principal)
    {
        principal = null;

        // *** THIS IS YOUR CUSTOM DATABASE STUFF.
        //     CHANGE THIS WITH .. ER .. WHATEVER YOU'RE USING.
        var user = myDatabase.FindUserByToken(token);

        if (user == null)
        {
            return false;
        }

        var identity = new GenericIdentity(_tokensAndNames[token], AuthenticationType);
        principal = new GenericPrincipal(identity, null);

        return true;
    }

    #endregion
}

[TokenAuthorize(Authorization = new MyDatabaseRequestHeaderAuthorization(myDatabase),
                RequireSsl=true, Header="XXX-Token")]
public ActionResult Post(PostInputModel postInputModel)
{ ... }

Ok .. so here we are saying that

  • We need to have a secure request
  • The header key we'll look for is XXX-Token
  • We ask the database to return the user with the provided token, assuming one was provided correctly.

Curl: curl -H "XXX-Token: qwerty" https://api.PewPew.com

Pull Requests?

Awww yeah! I do accept em hint thint

worlddomination.web.mvc.requestheaderauthorizeattribute's People

Contributors

purekrome avatar

Stargazers

 avatar Tom Mitchell avatar

Watchers

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