zoukankan      html  css  js  c++  java
  • NancyFx 2.0的开源框架的使用-Basic

    这是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类里面

    谢谢各位的观看!

  • 相关阅读:
    1分钟快速生成用于网页内容提取的xslt
    Python即时网络爬虫项目: 内容提取器的定义
    Python读取PDF内容
    Golang基础(二)
    shell的sed命令
    matplotlib + pandas绘图
    关于字符编码:ascii、unicode与utf-8
    shell的sort命令
    shell的uniq命令
    shell的tr命令
  • 原文地址:https://www.cnblogs.com/R00R/p/6838014.html
Copyright © 2011-2022 走看看