gpt4 book ai didi

forms-authentication - Nancy:FormsAuthentication - 入门

转载 作者:行者123 更新时间:2023-12-02 07:26:22 25 4
gpt4 key购买 nike

我正在尝试遵循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 session 提供程序并使用它来查找和验证用户

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,但是将其作为一个属性是一个好的做法吗? User 类中的字段?)

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; }
}

最后,我通过直接从演示中窃取代码(上面的链接)为我的 Bootstrap 添加了更多设置。这就是我遇到问题的地方。代码似乎已更改。

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 语句 - 请参阅下面的评论)。上面的所有代码现在都可以正常工作,所以我将保留这个问题。

最佳答案

以防万一您错过了上面的评论,答案非常简单:

Gotcha! OK, I found the problem with the broken code. Resharper helpfully put in the following using statement: 'using FormsAuthenticationConfiguration = System.Web.Configuration.FormsAuthenticationConfiguration;'. Removing this solved the broken code :-) However I would still welcome any comments about my implementation. I need reassurance that I am on the right path.

关于forms-authentication - Nancy:FormsAuthentication - 入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com