C# 自托管Nancy:Nancy.Authentication.Forms.dll中输入正确的用户/密码时发生NullReferenceException

C# 自托管Nancy:Nancy.Authentication.Forms.dll中输入正确的用户/密码时发生NullReferenceException,c#,nancy,C#,Nancy,我正在尝试使用自托管Nancy创建表单验证。为了简单起见,用户数据没有db,它存储在列表中。我们有两个用户: U:管理员p:密码 U:用户p:密码 我正在使用: 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 我的登录模块: Get["/login"] = x =>

我正在尝试使用自托管Nancy创建表单验证。为了简单起见,用户数据没有db,它存储在列表中。我们有两个用户:

U:管理员p:密码

U:用户p:密码

我正在使用:

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
我的登录模块:

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);
当输入错误的用户/密码时,一切正常。当输入正确的用户/密码时,在LoginAndRedirect发生NullReference异常:

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
调用堆栈:

>   NancyLinuxTest.exe!NancyLinuxTest.Models.MainModule..ctor.AnonymousMethod__16(dynamic x) Line 49    C#
堆栈跟踪:

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)
Nancy.Authentication.Forms.FormsAuthentication.EncryptAndSignCookie(字符串cookieValue,FormsAuthenticationConfiguration)\r\n位于Nancy.Authentication.Forms.FormsAuthentication.BuildCookie(Guid用户标识符,可为null的'1 CookieExpirement,FormsAuthenticationConfiguration)\r\n位于Nancy.Authentication.Forms.FormsAuthentication.UserLoggedInRedirectResponse(Nancy上下文,Guid userIdentifier,Nullable`1 CookieExpire,String fallbackRedirectUrl)\r\n位于Nancy.Authentication.Forms.ModuleExtensions.LoginAndRedirect(在模块中,Guid userIdentifier,Nullable`1 CookieExpirement,String fallbackRedirectUrl)\r\n位于NancyLinuxTest.Models.MainModule.b_uu16(对象x)的d:\\prototype prices\\for_delete\\\ proto\\NancyFormAuthTest\\NancyFormAuthTest\\Modules\\MainModule.cs:CallSite.Target(Closure,CallSite,Func`2,Object)的第55行\r\n位于Nancy.Routing.Routing.Route.c_u显示类4.b_u3(对象参数,取消令牌上下文)
userGuid。值不为null

唯一可以触发NRE(in)的行是
配置
遵从性。如果查看代码,这是通过调用来设置的。从那里开始调查,查看何时调用
Enable
,检查传入的配置


免责声明:我不知道南希是什么,也不在乎。这是您应该进行的基本代码调试。都是开源的。只需仔细检查一下。

发现了我的问题,我调用了错误的引导程序:)。

NullReferenceException发生在库中,因此不是标准NRE问题的重复。该行的userGuid的准确值为55e1e49e-b7e8-4eea-8459-7a906ac4d4c0。这与用户列表中的Guid相同@mjwills那是一个好主意。这是我讨厌这个框架的原因之一,调试它简直是地狱。它到处抛出NRE和“Oops!”es,开发人员多次声称“如果出现错误,代码就很复杂。请简化代码”。这并不是一种实用的编程方法。OP可能忘记了在启动时注册或调用某些东西,然后框架在运行时崩溃。NancyLinuxTest.exe!NancyLinuxTest.Models.MainModule..ctor.AnonymousMethod_uu16(动态x)第49行C#
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);
}