• NancyFx 2.0的开源框架的使用-Authentication


    新建一个空的项目

    新建好了空的项目以后,接着通过NuGet安装一下三个包

    • Nancy
    • Nancy.Hosting.Aspnet
    • Nancy.ViewEnglines.Razor

    然后在项目中添加Models,Module,Views三个文件夹,并在Models中添加UserModel类

            public string  Username { get; set; }
            public UserModel(string username)
            {
                this.Username = username;
            }

    然后往Module文件夹里面添加MainModule类

     Get("/", Lexan => { return View["index.cshtml"]; });
                Get("/login", Lexan => { return View["login.cshtml",this.Request.Query.returnUrl]; });

    再继续添加SecureModule类,AnotherVerySecureModule类

            public SecureModule():base("/secure")
            {
                this.RequiresAuthentication();
                Get("/",Lexan=>
                {
                    var model = new UserModel(this.Context.CurrentUser.Identity.Name);
                    return View["secure.cshtml",model];
                });
            }

     public AnotherVerySecureModule():base("/superSecure")
            {
                this.RequiresClaims(Lexan=>Lexan.Type==ClaimTypes.Role&&Lexan.Value=="SuperSecure");
                Get("/",Lexan=> 
                {
                    var model = new UserModel(this.Context.CurrentUser.Identity.Name);
                    return View["superSecure.cshtml",model];
                });
            }

    根目录添加AuthenticationBootstrapper类

            protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
            {
                base.ApplicationStartup(container, pipelines);
                pipelines.BeforeRequest += ctx =>
                  {
                      var username = ctx.Request.Query.username;
                      if (username.HasValue)
                      {
                          ctx.CurrentUser = new ClaimsPrincipal(new ClaimsIdentity(BuildClaims(username), "querystring"));
                      }
                      return null;
                  };
                pipelines.AfterRequest += ctx =>
                  {
                      if (ctx.Response.StatusCode==HttpStatusCode.Unauthorized)
                      {
                          ctx.Response = new RedirectResponse("/login?retutnUrl="+ Uri.EscapeDataString(ctx.Request.Path));
                      }
                  };
            }
            private static IEnumerable<Claim> BuildClaims(string userName)
            {
                var claims = new List<Claim>();
                if (String.Equals(userName,"Lexan",StringComparison.OrdinalIgnoreCase))
                {
                    claims.Add(new Claim(ClaimTypes.Role,"SuperSecure"));
                }
                return claims;
            }

    继续在Views里添加视图index,login,secure,superSecure

    再然后修改一下Web.config如下图

    运行如下图

    谢谢观看!

  • 相关阅读:
    地理大发现
    克里斯托弗·哥伦布
    2016. last day in office
    泰斯花粉阻隔剂 怎么使用
    ZT 螨虫知识2
    ZT 螨虫的话就不要跟狗多接触,狗的寄生虫很多,还有草地,
    expense KK [ɪkˋspɛns] DJ [iksˋpens]
    Windows 实战项目 001 文件扫描器 (01)
    017 系统内存信息 内存大小 空闲内存 5
    017 虚拟内存页面区块 4
  • 原文地址:https://www.cnblogs.com/R00R/p/6832931.html
Copyright © 2020-2023  润新知