簡體   English   中英

登錄不記得Nancy.Authentication.Forms?

[英]Login not remembered in Nancy.Authentication.Forms?

在過去的幾個星期里,這個問題一直在亂問,但是現在我真的需要解決這個問題,我似乎無法理解它。

簡而言之:我有一個啟用了表單身份驗證的nancy應用程序。 所有工作都很好,期望會話在應用重啟之間不會持續存在,所以看起來如此。 這應該有效,因為表單身份驗證默認使用cookie。 你們有什么想法會導致這種行為嗎? 這是我的代碼:

Bootstrapper.cs:

    protected override void RequestStartup(TinyIoCContainer container, IPipelines           pipelines, NancyContext context)
    {
        base.RequestStartup(container, pipelines, context);
            var formsAuthConfiguration = new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/user/login",
                UserMapper = container.Resolve<IUserMapper>()
            };
            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
    }

並在應用程序啟動:

    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        Nancy.Session.CookieBasedSessions.Enable(pipelines);
        //Nancy.Session.MemoryCacheBasedSessions.Enable(pipelines); <-- disabled just to be sure
        Nancy.Json.JsonSettings.RetainCasing = true;
        Nancy.Json.JsonSettings.MaxJsonLength = Int32.MaxValue;
        StaticConfiguration.DisableErrorTraces = false;
        Elmahlogging.Enable(pipelines, "elmah");
        //Some background job initialization...
    }

處理登錄/ POST請求的het模塊中的路由:

        Post["/login"] = parameters =>
        {
            VerifyUserViewModel userLoginData = this.Bind();
            var verified = userManager.VerifyAccount(userLoginData.Email, userLoginData.Password);
            userLoginData.LoginFailed = false;
            if (verified == false)
            {
                userLoginData.LoginFailed = true;
                return View["SignIn", userLoginData];
            }
            else
            {
                var user = userManager.GetByEmail((string)Request.Form.Email);
                DateTime? expiry = null;
                if (this.Request.Form.RememberMe.HasValue)
                {
                    expiry = DateTime.Now.AddDays(30);
                }
                return this.LoginAndRedirect(user.Guid, expiry, "/dash");
            }
        };

最后,IUserMapper實現:

public class UserDatabase : IUserMapper
{
    private readonly IUserManager _userManager;
    public UserDatabase(IUserManager userManager)
    {
        _userManager = userManager;
    }

    public Nancy.Security.IUserIdentity GetUserFromIdentifier(Guid identifier, Nancy.NancyContext context)
    {
        var user = (ReflectUser)_userManager.GetByGuid(identifier);
        var identity = new ReflectWebUser();
        identity.Map(user);
        return identity;
    }
}

你們注意到我的代碼有什么奇怪的東西會阻礙會話持久性嗎?

請注意,我也在此應用程序中使用基於令牌的身份驗證,但我已將其完全禁用以進行測試。 此外,在實現令牌驗證之前存在此問題。

謝謝!

讓鏈接死掉。

問題是Nancy在應用程序啟動時生成加密密鑰,這意味着當您在開發期間重建應用程序並重新請求頁面時,cookie檢查將失敗並被刪除,從而導致用戶顯示未經身份驗證。

如果IIS被回收,應用程序將重新啟動,生成新密鑰,bam用戶將被注銷,也會發生同樣的情況。

解決方案是自己生成一個特定的密鑰。 配置Forms Auth時,您可以創建自己的加密配置:

var cryptographyConfiguration = new CryptographyConfiguration(
    new RijndaelEncryptionProvider(new PassphraseKeyGenerator("SuperSecretPass", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 })),
    new DefaultHmacProvider(new PassphraseKeyGenerator("UberSuperSecure", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 })));

var config = 
    new FormsAuthenticationConfiguration()
    {
        CryptographyConfiguration = cryptographyConfiguration,
        RedirectUrl = "/login",
        UserMapper = container.Resolve<IUserMapper>(),
    };

這將在開發和應用程序回收期間持續登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM