Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy: FormsAuthentication - Getting Started

I am trying to follow the Nancy.Demo.Authentication.Forms example but I am running into problems as it looks like the example code is now out of date. I am sorry if this question is long but I don't want to miss out my mistakes. So here is what I have done so far:

I successfully installed the authentication package via the package manager console (VS11 beta)

PM> install-package nancy.Authentication.Forms
Attempting to resolve dependency 'Nancy (= 0.10.0)'.
Successfully installed 'Nancy.Authentication.Forms 0.10.0'.
Successfully added 'Nancy.Authentication.Forms 0.10.0' to uMentor.

I wrote an implementation of IUserMapper that takes a dependency on my RavenDB session provider and uses that to find and validate users

public class FormsAuthenticationService : IUserMapper
{
    private readonly IRavenSessionProvider _ravenSessionProvider;

    public FormsAuthenticationService(IRavenSessionProvider ravenSessionProvider)
    {
        _ravenSessionProvider = ravenSessionProvider;
    }

    public IUserIdentity GetUserFromIdentifier(Guid identifier)
    {
        using (var ravenDB = _ravenSessionProvider.GetSession())
        {
            var user = ravenDB.Query<User>().FirstOrDefault(u => u.FormsAuthenticationGuid == identifier);
            return user;
        }
    }

    public static Guid? ValidateUser(IDocumentSession ravenDB, string username, string password)
    {
        var user = ravenDB.Query<User>().FirstOrDefault(u => u.UserName == username && u.Password == password);
        if (user == null)
        {
            return null;
        }
        return user.FormsAuthenticationGuid;
    }
}

I have added a property to my User class to cater for the Guid identifier field required to make the cookie more secure (I have read grumpydev's posts and understand why this Guid is needed, but is it good practice to make this a property field on the User class?)

public class User : IUserIdentity
{
    public string UserName { get; set; }
    public IEnumerable<string> Claims { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public Guid FormsAuthenticationGuid { get; set; }
}

Finally I have added more setup to my bootstrapper by stealing the code directly out of the Demo (link above). This is where I am getting problems. The code appears to have changed.

public class MyBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
    {
        base.ConfigureRequestContainer(container, context);
        container.Register<IUserMapper, FormsAuthenticationService>();
    }

    protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
    {
        var formsAuthConfiguration =
            new FormsAuthenticationConfiguration()
            {
                //These properties do not exist! <<---- Edit - yes they do - see comment 
                RedirectUrl = "~/login",
                UserMapper = requestContainer.Resolve<IUserMapper>(),
            };
        //This method does not exist <<---- Edit - yes they do - see comment
        FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
    }

    protected override NancyInternalConfiguration InternalConfiguration
    {
        get { return NancyInternalConfiguration.WithOverrides(x => x.NancyModuleBuilder = typeof(RavenAwareModuleBuilder)); }
    }
}

EDIT 1 It turns out my mistake was silly (an incorrect using statement - see comments below). All the code above now works just fine so I will leave this question standing.

like image 601
biofractal Avatar asked Apr 17 '12 10:04

biofractal


1 Answers

Just in case you missed the comment above, the answer was pathetically simple:

Gotcha! OK, I found the problem with the broken code. Resharper helpfully put in the following using statement: 'using FormsAuthenticationConfiguration = System.Web.Configuration.FormsAuthenticationConfiguration;'. Removing this solved the broken code :-) However I would still welcome any comments about my implementation. I need reassurance that I am on the right path.

like image 65
biofractal Avatar answered Oct 10 '22 20:10

biofractal