How do I even begin coding authentication using ASP.NET Web API so it is cross-platform to support desktop, mobile and web? I’d read of some methods of doing RESTful authentication, such as using tokens in the header.
Are there any example projects out there that utilizes this method?
Questions:
- If not how do I fix the
[Authorize]attribute to read the token? - How do I generate this token? I dont think i can use formsauthentication because that uses cookies.
- How do I handle the actual authorization, do the client send raw password and username then I generate the token or is there some other way?
- How do I handle when my website is using it? I heard this is handled differently than when an app is using it, such as getting the domain and authorizing it.
I think tokens would be a solid way to go. Forms authentication is based on cookies for the web. Not the most idea situation for all non browser clients though.
What I’d suggest is creating a custom AuthorizationFilterAttribute and overriding the OnAuthorization method. In that method, you could check for the existence of a token that you’ve issued to the client after they’ve supplied valid credentials. You can use this attribute on any method or controller you want validated. Here’s a sample you might reference
TOKEN_HEADER is just a string representing an HTTP header that the client should pass back for authenticated requests.
So let’s walk through it
Also, check this post by John Petersen. Making your ASP.NET Web API’s secure