代码之家  ›  专栏  ›  技术社区  ›  B-Z

自托管Nancy:Nancy中发生NullReferenceException。身份验证。形式。输入正确的用户/密码时的dll

  •  0
  • B-Z  · 技术社区  · 7 年前

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

    U: 管理员P:passowrd

    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发生NullReferenceException:

    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)
    

    用户GUID。值不为null。

    Full source here

    2 回复  |  直到 7 年前
        1
  •  1
  •   B-Z    7 年前

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

        2
  •  0
  •   Remus Rusanu    7 年前
    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);
    }
    

    唯一可以触发NRE(in)的行 v.1.4.1 )是 configuration 尊敬如果查看代码,则通过调用 Enable . 开始调查,看看什么时候 使可能 调用时,检查传入的配置。

    免责声明:我不知道南希是什么,也不在乎。这是基本的代码调试 应该这样做。都是开源的。只管走过去。