Search code examples
c#nancy

Selfhosted Nancy: NullReferenceException occurred in Nancy.Authentication.Forms.dll When correct user/password is entered


I am trying to create a form auth with self hosted Nancy.To make it simple there is no db for user data, it is stored in a List.We have two users:

U: admin P: passowrd

U: user P: password

I am using:

Nancy.1.4.4
Nancy.Authentication.Forms.1.4.1
Nancy.Hosting.Self.1.4.1
Nancy.Viewengines.Razor.1.4.3
Microsoft.AspNet.Razor.2.0.30506.0

My login module:

Get["/login"] = x =>
            {
                Model.login = new LoginModel() { Error = this.Request.Query.error.HasValue, ReturnUrl = this.Request.Url };
                return View["login", Model];
            };

            Post["/login"] = x =>
            {
                var userGuid = MyUserMapper.ValidateUser((string) this.Request.Form.Username,
                    (string) this.Request.Form.Password);

                if (userGuid == null)
                {
                    return Context.GetRedirect("~/login?error=true&username=" +
                                               (string) this.Request.Form.Username);
                }

                DateTime? expiry = null;
                if (this.Request.Form.RememberMe.HasValue)
                {
                    expiry = DateTime.Now.AddDays(7);
                }


                return this.LoginAndRedirect(userGuid.Value, expiry);

When a wrong user/password is entered everything is ok.When a correct user/password is entered NullReferenceException occurs at LoginAndRedirect:

return this.LoginAndRedirect(userGuid.Value, expiry);


An exception of type 'System.NullReferenceException' occurred in Nancy.Authentication.Forms.dll but was not handled in user code

Call Stack:

>   NancyLinuxTest.exe!NancyLinuxTest.Models.MainModule..ctor.AnonymousMethod__16(dynamic x) Line 49    C#

Stack Trace:

Nancy.Authentication.Forms.FormsAuthentication.EncryptAndSignCookie(String cookieValue, FormsAuthenticationConfiguration configuration)\r\n   at Nancy.Authentication.Forms.FormsAuthentication.BuildCookie(Guid userIdentifier, Nullable`1 cookieExpiry, FormsAuthenticationConfiguration configuration)\r\n   at Nancy.Authentication.Forms.FormsAuthentication.UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, Nullable`1 cookieExpiry, String fallbackRedirectUrl)\r\n   at Nancy.Authentication.Forms.ModuleExtensions.LoginAndRedirect(INancyModule module, Guid userIdentifier, Nullable`1 cookieExpiry, String fallbackRedirectUrl)\r\n   at NancyLinuxTest.Models.MainModule.<.ctor>b__16(Object x) in d:\\prototype-prices\\for_delete\\#proto\\NancyFormAuthTest\\NancyFormAuthTest\\Modules\\MainModule.cs:line 55\r\n   at CallSite.Target(Closure , CallSite , Func`2 , Object )\r\n   at Nancy.Routing.Route.<>c__DisplayClass4.<Wrap>b__3(Object parameters, CancellationToken context)

userGuid.Value is not null.

Full source here


Solution

  • Found my problem, I was calling the wrong Bootstrapper :).