我正在尝试使用自托管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