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

    谢谢各位的观看!

  • 相关阅读:
    python 中给文件加锁——fcntl模块
    python生成二维码
    uwsgi常用配置
    php curl实现get和post请求
    python __enter__ 与 __exit__的作用,以及与 with 语句的关系
    python文件操作总结
    Python时间,日期,时间戳之间转换
    Python random模块(获取随机数)
    wigs的理解和应用
    shiro中接入单点登录功能
  • 原文地址:https://www.cnblogs.com/R00R/p/6838014.html
Copyright © 2011-2022 走看看