我正在尝试遵循Nancy.Demo.Authentication.Forms示例,但我遇到了问题,因为示例代码现在看起来已经过时了。如果这个问题很长,我很抱歉,但我不想错过我的错误。所以这是我到目前为止所做的:
我通过包管理器控制台(VS11 beta)成功安装了认证包
PM> install-package nancy.Authentication.Forms
Attempting to resolve dependency 'Nancy (= 0.10.0)'.
Successfully installed 'Nancy.Authentication.Forms 0.10.0'.
Successfully added 'Nancy.Authentication.Forms 0.10.0' to uMentor.
我编写了 IUserMapper 的实现,它依赖于我的 RavenDB 会话提供程序并使用它来查找和验证用户
public class FormsAuthenticationService : IUserMapper
{
private readonly IRavenSessionProvider _ravenSessionProvider;
public FormsAuthenticationService(IRavenSessionProvider ravenSessionProvider)
{
_ravenSessionProvider = ravenSessionProvider;
}
public IUserIdentity GetUserFromIdentifier(Guid identifier)
{
using (var ravenDB = _ravenSessionProvider.GetSession())
{
var user = ravenDB.Query<User>().FirstOrDefault(u => u.FormsAuthenticationGuid == identifier);
return user;
}
}
public static Guid? ValidateUser(IDocumentSession ravenDB, string username, string password)
{
var user = ravenDB.Query<User>().FirstOrDefault(u => u.UserName == username && u.Password == password);
if (user == null)
{
return null;
}
return user.FormsAuthenticationGuid;
}
}
我已经在我的 User 类中添加了一个属性来满足使 cookie 更安全所需的 Guid 标识符字段(我已经阅读了 grumpydev 的帖子并理解为什么需要这个 Guid,但是将它作为一个属性字段在用户等级?)
public class User : IUserIdentity
{
public string UserName { get; set; }
public IEnumerable<string> Claims { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public Guid FormsAuthenticationGuid { get; set; }
}
最后,我通过直接从演示中窃取代码(上面的链接)为我的引导程序添加了更多设置。这是我遇到问题的地方。代码似乎已更改。
public class MyBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
base.ConfigureRequestContainer(container, context);
container.Register<IUserMapper, FormsAuthenticationService>();
}
protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
{
var formsAuthConfiguration =
new FormsAuthenticationConfiguration()
{
//These properties do not exist! <<---- Edit - yes they do - see comment
RedirectUrl = "~/login",
UserMapper = requestContainer.Resolve<IUserMapper>(),
};
//This method does not exist <<---- Edit - yes they do - see comment
FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
}
protected override NancyInternalConfiguration InternalConfiguration
{
get { return NancyInternalConfiguration.WithOverrides(x => x.NancyModuleBuilder = typeof(RavenAwareModuleBuilder)); }
}
}
编辑 1
事实证明我的错误是愚蠢的(一个不正确的using
陈述 - 见下面的评论)。上面的所有代码现在都可以正常工作,所以我将保留这个问题。