I am trying to follow the example of Nancy.Demo.Authentication.Forms , but I ran into problems because it looks like the sample code is now deprecated. Sorry if this question is long, but I do not want to miss my mistakes. So here is what I have done so far:
I have successfully installed the authentication package through 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 IUserMapper implementation that depends on my RavenDB session provider and uses this to search and verify 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 added a property of my User class to serve the Guid identifier field needed to make the cookie more secure (I read the grumpydev messages and realized why this Guid is needed, but is it good practice to make this property a field in 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 added more settings to my boot file, stealing it directly from the demo (link above). This is where I get the problems. The code seems 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() {
EDIT 1 It turns out my mistake was stupid (wrong using
statement - see comments below). All of the above code now works fine, so I will leave this question unanswered.