Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to login with attribute Authorize using cookies authentication in ASP.NET 5

I am testing the [Authorize] attribute, but I can't make a redirect to login page if the user has not logged yet (the Chrome inspector returns a 401).

This is my code to make the login in my Controller (very simple).

if (model.UserName == "admin" && model.Password == "test")
{
    var claims = new[] { new Claim("name", model.UserName), new Claim(ClaimTypes.Role, "Admin") };
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
    return RedirectToAction("Index", "Home");
}

And this is my configuration in the Startup.cs for logins:

app.UseCookieAuthentication(options =>
    {
        options.AutomaticAuthenticate = true;
        options.LoginPath = new PathString("/Account/Login");
    });

Any ideas?

Thanks!!

like image 460
chemitaxis Avatar asked Dec 23 '15 14:12

chemitaxis


People also ask

Which authentication uses Cookies for user authentication?

Cookie authentication uses HTTP cookies to authenticate client requests and maintain session information. It works as follows: The client sends a login request to the server.

What is cookie-based authentication in C#?

A Cookie-based authentication uses the HTTP cookies to authenticate the client requests and maintain session information on the server over the stateless HTTP protocol. Here is a logical flow of the cookie-based authentication process: The client sends a login request with credentials to the backend server.

What does HttpContext SignInAsync do?

SignInAsync(HttpContext, String, ClaimsPrincipal, AuthenticationProperties) Sign in a principal for the specified scheme.


2 Answers

Your Startup.cs should look like the following:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    LoginPath = "/account/login",

    AuthenticationScheme = "Cookies",
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
});

Setting the AutomaticChallenge is what is going to make the [Authorize] attribute work. Be sure to include the [Authorize] attribute on any of the controllers you want the redirect (302) to happen.

There is a very basic sample in this GitHub repo that might provide some guidance: https://github.com/leastprivilege/AspNet5TemplateCookieAuthentication

like image 193
Roberto Hernandez Avatar answered Oct 28 '22 17:10

Roberto Hernandez


Try this in the Startup.cs:

app.UseCookieAuthentication(options =>
{
   options.AuthenticationType = CookieAuthenticationDefaults.AuthenticationScheme;
   options.AutomaticAuthenticate = true;
   options.AutomaticChallenge = true;
   options.LoginPath = new PathString("/Account/Login");
});

And this in the Controller

IAuthenticationManager authManager = Request.GetOwinContext().Authentication;
authManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
like image 28
Fabio Luz Avatar answered Oct 28 '22 17:10

Fabio Luz