NancyFx 2.0的開源框架的使用-Basic
阿新 • • 發佈:2017-05-11
password return public 文件夾 null
這是NancyFx開源框架中的Basic認證,學習一下!
首先當然是新建一個空的Web,BasicDemo
繼續在項目中添加Nuget包,記得安裝的Nuget包是最新的預發行版
Nancy
Nancy.Authentication.Basic
Nancy.Hosting.Aspnet
之後就往項目中添加Models文件夾和Module文件夾,然後往Models文件夾裏面添加UserValidator類
public ClaimsPrincipal Validate(string username,string password) { if (username=="Lexan"&&password=="password") { return new ClaimsPrincipal(new GenericIdentity(username)); } //沒有認證=>匿名 return null; }
繼續在Module文件裏面添加MainModule類
public MainModule()
{
Get("/",Lexan=>"<a href=‘/secure‘>地址欄輸入/secure訪問Secure頁面</a>");
}
繼續往Module文件夾裏面添加SecureModule類
public SecureModule() : base("/secure")
{
this.RequiresAuthentication();
Get("/", args => "Hello " + this.Context.CurrentUser.Identity.Name);
}
然後就在根目錄添加BasicBootstrapper類,用來初始化項目的
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { base.ApplicationStartup(container, pipelines); pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(container.Resolve<IUserValidator>(),"Lexan")); }
運行一下寫好的項目,登陸賬號和密碼寫在了UserValidator類裏面
NancyFx 2.0的開源框架的使用-Basic