自托管南希:在 Nancy.Authentication.Forms.dll 输入正确的 user/password 时发生 NullReferenceException
Selfhosted Nancy: NullReferenceException occurred in Nancy.Authentication.Forms.dll When correct user/password is entered
我正在尝试创建一个自我托管的表单验证 Nancy.To 简单来说,没有用于用户数据的数据库,它存储在 List.We 有两个用户:
U: admin 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);
当输入错误的 user/password 时,一切正常 ok.When 输入正确的 user/password NullReferenceException 发生在 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
调用堆栈:
> 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)
userGuid.Value 不为空。
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 的行(在 v.1.4.1) is the configuration
deference. If you look in the code this is set by calling Enable
中。从那里开始调查,查看何时调用 Enable
,检查传入的配置。
免责声明:我不知道南希是什么,也不关心。这是基本的代码调试 you 应该做的。都是开源的。只需逐步完成即可。
发现我的问题,我调用了错误的 Bootstrapper :)。
我正在尝试创建一个自我托管的表单验证 Nancy.To 简单来说,没有用于用户数据的数据库,它存储在 List.We 有两个用户:
U: admin 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);
当输入错误的 user/password 时,一切正常 ok.When 输入正确的 user/password NullReferenceException 发生在 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
调用堆栈:
> 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)
userGuid.Value 不为空。
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 的行(在 v.1.4.1) is the configuration
deference. If you look in the code this is set by calling Enable
中。从那里开始调查,查看何时调用 Enable
,检查传入的配置。
免责声明:我不知道南希是什么,也不关心。这是基本的代码调试 you 应该做的。都是开源的。只需逐步完成即可。
发现我的问题,我调用了错误的 Bootstrapper :)。