Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bearer tokens and cookie authentication together

I have a single page app - more or less based on the MVC5 SPA template - using bearer tokens for authentication.

The site also has a couple of conventional MVC pages which need to be secured, but using cookie authentication.

In Startup.Auth I can enable both types of authorisation:

app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOAuthBearerTokens(OAuthOptions); 

However, this seems to have a side-effect in that whenever an AJAX request is sent from the SPA, it sends both the bearer token in the header and the cookie.

Whereas the behaviour I really want is that only the bearer token is used for WebAPI calls, and only the cookie for MVC calls.

I'd also like the MVC calls to redirect to a login page when not authorised (set as a CookieAuthenticationOption), but obviously I don't want this to happen when making an API call.

Is there some way to have this type of mixed-mode authentication within one application? Perhaps through a path/route filter?

like image 336
Appetere Avatar asked Jan 06 '14 15:01

Appetere


People also ask

Can I use JWT with cookies?

On every request to server, the JWT will be read from Cookies and added in the Authorization header using Bearer scheme. The server can then verify the JWT in the request header (as opposed to reading it from the cookies).

What is the problem when using cookies for authentication?

Limitations of cookie-based authenticationIt is vulnerable to Cross-site request forgery attack. It often needs other security measures such as CSRF tokens for protection. You need to store the session data in a database or keep it in memory on the server.

Can token be stored in cookie?

There are two common ways to store your tokens. The first is in localStorage and the second is in cookies. There is a lot of debate over which one is better with most people leaning toward cookies as they are more secure.

Can cookies be used for user authentication?

Specific cookies like HTTP cookies are used to perform cookie-based authentication to maintain the session for each user. The entire cookie-based authentication works in the following manner: The user gives a username and password at the time of login.


1 Answers

I think I worked this out:-

Startup.Auth is wiring up the OWIN pipeline, so it is right to include Cookies and Tokens there. But one change to the cookie options specifies the authentication type it should apply to:

CookieOptions = new CookieAuthenticationOptions {   AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie    }; 

Then I needed to configure WebAPI to only use tokens:

public static void Configure(HttpConfiguration config) {    // Configure Web API to use only bearer token authentication.    config.SuppressDefaultHostAuthentication();    config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); } 

This seems to achieve what I want. WebAPI just uses bearer tokens and no cookies, and a few conventional MVC pages use cookies once logged in (using the AuthenticationManager).

like image 125
Appetere Avatar answered Oct 04 '22 16:10

Appetere