3

We are working on a ASP.NET vNext project and we are implementing simple cookie-based authentication.

We thought about using the auth-cookie just to verify the user is authenticated. It's created like this:

HttpContext.Authentication.SignInAsync();

The user-object itself would be saved to the session-cache (ISession).

  • Is this a practise one could use or are we thinking about this the entirely the wrong way?
  • If the basic idea is correct, how do we keep the auth-cookie and the session in sync? (An unauthenticated user with a user-object in the session-cache would be bad)

Note that not only the user-object should be stored in the session. There is a bunch of information that should be stored there (only if the user is authenticated).

Thanks for your input!

1
  • Have you made any progress? Commented Apr 18, 2016 at 8:51

1 Answer 1

1

Considering your requirements is it an option for you to use Cookie middleware to persist the user between requests and use Claims-based authorization to persist additional info you want to remember about the user?

So in the Startup class in the Configure method you'd add the cookie middleware for instance like:

            app.UseCookieAuthentication(options =>
        {
            options.AuthenticationScheme = "Cookies";
            options.AutomaticAuthenticate = true;
            options.AutomaticChallenge = true;
            options.LoginPath = new PathString(@"/account/login");
            options.AccessDeniedPath = new PathString(@"/account/accessdenied");

        });

And then in ConfigureServices you can set the claims you require like for instance this:

   services.AddAuthorization(options =>
{
    options.AddPolicy("EmployeeOnly", policy => policy.RequireClaim("EmployeeNumber"));
});

Please consider the links for complete documentation about how to set this up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.