Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

Forms authentication per module can not work

Open liupengf12 opened this issue 10 years ago • 5 comments

Forms authentication per module can not work

  • [x] I have written a descriptive issue title
  • [ ] I have verified that I am running the latest version of Nancy
  • [ ] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [ ] I have searched open and closed issues to ensure it has not already been reported

Description

I need the forms authentication only works for one of my special module, but it did not redirect to the login page and only return a HTTP code with 401. Is there any mistake in my codes?

Steps to Reproduce

I wrote the codes like this:

protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
{
    base.RequestStartup(requestContainer, pipelines, context);

    var waiterModule = GetModule(typeof(WaitersModule), context);
    FormsAuthentication.Enable(waiterModule, new FormsAuthenticationConfiguration {
        RedirectUrl = "~/waiters/login",
        UserMapper = requestContainer.Resolve<IUserMapper>()
    });
}

And the module like this:

public WaitersModule() : base("/waiters")
{
    Get["/profile"] = parameters =>
    {
        this.RequiresAuthentication();

        return View["profile", model];
    };
}

System Configuration

  • Nancy version: 1.4.3
  • Nancy host
    • [ ] ASP.NET
    • [x] OWIN
    • [ ] Self-Hosted
    • [ ] Other:
  • Other Nancy packages and versions: Nancy.Authentication.Forms v1.4.1
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 4.5.1
  • Additional information:

liupengf12 avatar May 08 '16 03:05 liupengf12

It may be a daft question but do you have a module handling your login page route?

Get["/login"] = parameters => {

GusBeare avatar May 24 '16 21:05 GusBeare

The problem is that you haven't enabled forms auth for the specific module instance.

Nancy modules are instantiated on each request, so you have to enable the forms auth in the constructor:

public WaitersModule(IUserMapper userMapper) : base("/waiters")
{
    FormsAuthentication.Enable(this, new FormsAuthenticationConfiguration {
        RedirectUrl = "~/waiters/login",
        UserMapper = userMapper
    });

    Get["/profile"] = parameters =>
    {
        this.RequiresAuthentication();

        return View["profile", model];
    };
}

khellang avatar May 24 '16 21:05 khellang

@khellang Thanks for your help, but I got a error after changed the codes as you suggest: image

liupengf12 avatar May 31 '16 13:05 liupengf12

Yeah, now you're going in a redirect loop. You're basically redirecting to the login page, which it looks like you've secured (?), so it redirects to itself, which again redirects to itself on and on and on, until you get an error because the returnUrl query parameter is too long.

khellang avatar May 31 '16 13:05 khellang

I know why redirect loop happened: all the actions of the module are require authentication after enable the forms auth in its constructor, so I have to change the RedirectUrl with a different module.

Finally I found another solution: Multiple forms authentication sections

Thanks for your help again.

liupengf12 avatar Jun 01 '16 00:06 liupengf12