2012-04-17 81 views
5

我想要遵循Nancy.Demo.Authentication.Forms的例子,但我遇到了问题,因为它看起来像示例代码现在已过时。如果这个问题很长,我很抱歉,但我不想错过我的错误。所以这是我迄今所做的:南希:FormsAuthentication - 入门

我顺利地通过包管理器控制台安装身份验证包(VS11测试版)

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类,以满足使饼干更安全所需要的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声明 - 见下面的评论)。上面的所有代码现在都可以正常工作,所以我会留下这个问题。

+0

哎呀!好的,我发现问题与破损的代码。 Resharper有用地使用了以下使用语句:'使用FormsAuthenticationConfiguration = System.Web.Configuration.FormsAuthenticationConfiguration;'。删除这个解决了破碎的代码:-)然而,我仍然欢迎任何关于我的实施的意见。我需要保证我走在正确的道路上。谢谢。 – biofractal 2012-04-17 11:07:12

+0

我的南希FormsAuthentication与RavenDB现在所有的作品。尽管我绕着房子绕过自己的尾巴,但我会留下这个问题,因为它可能会帮助别人。 – biofractal 2012-04-19 08:22:48

回答

1

万一你错过了上述评论,答案很简单得可怜:

疑难杂症!好的,我发现问题与破损的代码。 Resharper 有用地使用了以下使用语句:'使用 FormsAuthenticationConfiguration = System.Web.Configuration.FormsAuthenticationConfiguration;'。删除 这解决了破碎的代码:-)但是,我仍然欢迎任何关于我的实施的 评论。我需要保证我在 正确的道路上。