2013-06-04 20 views
2

我使用NancyFx來製作一個簡單的網站,用戶可以使用ajax控件登錄和註銷。使用NancyFx進行表單身份驗證

我已閱讀Forms Authentication with Nancy的文檔,我想我已經完成了所有必需的步驟。

  1. 安裝Nancy.Authentication.Forms包
  2. 實現一個IUserMapper
  3. 實現途徑來處理登錄和註銷
  4. 配置並啓用表單驗證

我有一個問題,即在呼叫登錄後,請求沒有當前用戶設置。 執行登錄後,我可以看到一個cookie集。但是用戶映射器沒有被調用。我已經嘗試請求路由與和out.RequiresAuthentication();仍然沒有用戶。

這是我第4步

public class Bootstrapper : DefaultNancyBootstrapper 
{ 
    protected override void ConfigureApplicationContainer(TinyIoCContainer container) 
    { 
     container.Register<ISessionFactory>((c, p) => SessionFactory.Factory); 
    } 

    protected override void ConfigureConventions(NancyConventions conventions) 
    { 
     base.ConfigureConventions(conventions); 

     conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("assets", @"content/assets")); 
     conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("application", @"content/application")); 
    } 

    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) 
    { 
     base.ConfigureRequestContainer(container, context); 
     container.Register<IUserMapper, UserMapper>(); 
    } 

    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) 
    { 
     base.RequestStartup(container, pipelines, context); 

     var formsAuthConfiguration = 
      new FormsAuthenticationConfiguration() 
      { 
       RedirectUrl = "~/", 
       UserMapper = container.Resolve<IUserMapper>() 
      }; 

     FormsAuthentication.Enable(pipelines, formsAuthConfiguration); 
    } 
} 

這裏是我的兩步登錄&註銷邏輯3.

Post["/login"] = x => 
     { 
      //Verify here, hardcode for testing 
      string email = "[email protected].com"; 

      User user = ExecuteCommand(new FetchUser(email)); 

      this.LoginWithoutRedirect(user.Session); 
      return new { email = user.Email, authorized = true, status = "okay" }; 
     }; 

     Post["/logout"] = x => 
     { 
      return this.Logout("~/"); 
     }; 

我IUsermapper只是查找用戶從數據庫中與給定的引導程序執行ID。 我可以看到它在RequestStartup解析IUserMapper時得到構建,但從未有任何對get GetUserFromIdentifier函數的調用。

任何幫助將是偉大的。

回答

1

由於您使用的是LoginWithoutRedirect擴展名,所以不會調用GetUserFromIdentifier方法。這不是登錄名稱,而是調用GetUserFromIdentifier,而是任何後續的重定向。

做的事情將是一個更常用的方法:

string email = "[email protected]"; 
User user = ExecuteCommand(new FetchUser(email)); 
this.LoginAndRedirect(user.Session); 

這不是預期的是,登錄路由就可以直接訪問。相反,用戶通常會請求受保護的資源,進行身份驗證,然後重定向到請求的資源。

幾個其他點:

當我想你的代碼我得到一個錯誤返回一個匿名類型。相反,我需要返回類型爲JSON,像這樣:

this.LoginWithoutRedirect(user.Session); 
return Response.AsJson(new 
{ 
    email = user.Email, 
    authorized = true, 
    status = "okay" 
}); 

這工作得很好,它記錄並返回你的匿名類型的JSON對象,因爲沒有重定向然後,正如所料,它不致電GetUserFromIdentifier

最後,您的/logout路線應該使用this.RequiresAuthentication()加以保護。這是有道理的,因爲只有經過身份驗證的用戶需要註銷。當GetUserFromIdentifier返回空值時它也會保護你 - 可能是因爲緩存的用戶已超時。 RequiresAuthentication檢測到該空用戶並重定向回Get["/login"]

+0

註銷處理程序的好處。我沒有返回anon類型的麻煩,我在前端將它作爲一個對象返回。這個問題的關鍵是你必須做重定向,你不能調用登錄並返回自己的數據。 – Cogslave