1

Currently I have a project in ASP.NET Core with a simple authentication.

The main code was generated with Yeoman and generator-aspnet.

The generated code has a Account Controller to authenticate the users.

Every time a user needs to log, asp.net redirect the user to http://domain/Account/Login.

Because the primary language of this site it's not english, I like to change the url to something like this: http://domain/usuarios/login

So, in Account Controller, I added a Route annotation on top of the class:

[Authorize]
[Route("usuarios")]
public class AccountController : Controller
{
    ...

    [HttpGet]
    [AllowAnonymous]
    [Route("login")]
    public IActionResult Login(string returnUrl = null)
    {
        ...
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidadeAntiForgeryToken]
    [Route("login")]
    public async Task<IActionResult> Login(LoginViewModel model, string returlUrl = null)
    {
        ...
    }
}

Now, if I go manually to http://domain/usuarios/login, the login page appears. But, If I go to a page that need authentication, ASP.NET Core still redirect-me to http://domain/Account/Login and return a 404 error.

Checking the project.json, I see in dependencies section, the package:

"dependencies": {
  "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
  "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
  ...
}

Opening the Startup class, I see in ConfigureServices and Configure methods the code app.UseIdentity();, but nothing about app.UseCookieAuthentication()

Here is a part of Configure method:

app.UseStaticFiles();
app.UseIdentity();

app.UseMvc(routes =>
{
  routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
});

So, where I update the code to redirect to the correctly url?

PS: the generated code it's a lot like https://shellmonger.com/2015/04/04/asp-net-vnext-identity-part-2-the-login-process/

2
  • Normally your code sends 401 status code to client(i didn't understand why redirect to Account/Login) because you didn't specify AutomaticChallenge = true,(default is false). Try to set AutomaticChallenge to true or use [Authorize(ActiveAuthenticationSchemes = "Cookie")] Oct 20, 2016 at 12:22
  • @ademcaglin I update the question. I see after post this question that the code has a 'app.UseIdentity()' Oct 20, 2016 at 13:35

2 Answers 2

3

if you are using cookie based authentication, update your startup.cs like this:

public void ConfigureServices(IServiceCollection services)
{       
    services.Configure<CookieAuthenticationOptions>(options =>
    {
        options.LoginPath = new PathString("/usuarios/login");
    });
}
1

You need to configure IdentityOptions in ConfigureServices method like below:

services.Configure<IdentityOptions>(opt =>
{
      opt.Cookies.ApplicationCookie.LoginPath = new PathString("/usuarios/login"); 
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.