0

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

B-Z
  • 31
  • 6
  • The NullReferenceException occurs inside a library and is therefore not a duplicate of the canonical NRE question. – CodeCaster Dec 21 '17 at 11:58
  • Exact value of userGuid at that line is 55e1e49e-b7e8-4eea-8459-7a906ac4d4c0 .Thats the same Guid as in the User List. [Source](https://github.com/NancyFx/Nancy/blob/be4f8d42076e4e568a3742715437868e6c7d05af/src/Nancy.Authentication.Forms/ModuleExtensions.cs) – B-Z Dec 21 '17 at 12:06
  • @mjwills that's a [Nancy extension method](https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Authentication.Forms/ModuleExtensions.cs). This is one of the reasons I hate the framework, debugging it is a hell. It throws NRE's and "Oops!"es all around, and the developer has claimed multiple times _"If you get errors, your code is complex. Simplify your code"_. That's not really a pragmatic approach to programming. The OP probably forgot to register or call something in the startup, and then the framework blows up at runtime. – CodeCaster Dec 21 '17 at 12:12
  • NancyLinuxTest.exe!NancyLinuxTest.Models.MainModule..ctor.AnonymousMethod__16(dynamic x) Line 49 C# – B-Z Dec 21 '17 at 12:21

2 Answers2

1

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

B-Z
  • 31
  • 6
0
private static string EncryptAndSignCookie(string cookieValue, FormsAuthenticationConfiguration configuration)
{
    var encryptedCookie = configuration.CryptographyConfiguration.EncryptionProvider.Encrypt(cookieValue);
    var hmacBytes = GenerateHmac(encryptedCookie, configuration);
    var hmacString = Convert.ToBase64String(hmacBytes);

    return String.Format("{1}{0}", encryptedCookie, hmacString);
}

The only line that can trigger NRE (in v.1.4.1) is the configuration deference. If you look in the code this is set by calling Enable. Start you investigation there, see when Enable is called, check what configuration gets passed in.

Disclaimer: I have no idea what Nancy is, nor do I care. This is basic code debugging you should be doing. Is all open source. Just step through it.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569