1. 程式人生 > >NancyFx 2.0的開源框架的使用-Basic

NancyFx 2.0的開源框架的使用-Basic

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