新建一个空的项目

新建好了空的项目以后,接着通过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如下图

运行如下图

谢谢观看!

NancyFx 2.0的开源框架的使用-Authentication的更多相关文章

  1. NancyFx 2.0的开源框架的使用-Basic

    这是NancyFx开源框架中的Basic认证,学习一下! 首先当然是新建一个空的Web,BasicDemo 继续在项目中添加Nuget包,记得安装的Nuget包是最新的预发行版 Nancy Nancy ...

  2. NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)

    NancyFx框架的自定义模块 新建一个空的Web项目 然后通过NuGet库安装下面的包 Nancy Nancy.Hosting.Aspnet 然后添加Models,Module,Views三个文件夹 ...

  3. NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)

    NancyFx框架中使用绑定模型 新建一个空的Web程序 然后安装Nuget库里面的包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Spark 并在We ...

  4. NancyFx 2.0的开源框架的使用-HosingOwin

    Nancy框架的Owin使用 先建一个空的Web项目 然后往Nuget库里面添加Nancy包 Nancy Nancy.Owin Nancy.ViewEnglines.Spark 然后添加Models, ...

  5. NancyFx 2.0的开源框架的使用-Forms

    同样的像前面2篇博文一样,每个项目的开始基本都是建个空的Web项目 在NuGet库中安装以下几个NuGet包 Nancy Nancy.Authentication.Forms Nancy.Hostin ...

  6. NancyFx 2.0的开源框架的使用-Stateless

    同样和前面一样新建一个空的Web项目,都在根目录添加Module,Models,Views文件夹 添加Nuget包 在Models文件夹里面添加UserModel类 public string Use ...

  7. NancyFx 2.0的开源框架的使用-Stateless(二)

    继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...

  8. NancyFx 2.0的开源框架的使用-AspnetBootstrapping

    新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...

  9. NancyFx 2.0的开源框架的使用-Caching

    新建一个空的Web项目,命名CachingDemo 然后添加三个Nuget安装包 Nancy Nancy.Hosting.Aspnet Nancy.ViewsEngines.Razor 然后往项目里面 ...

随机推荐

  1. 《转载-两篇很好的文章整合》Android中自定义控件

    两篇很好的文章,有相互借鉴的地方,整合到一起收藏 分别转载自:http://blog.csdn.net/xu_fu/article/details/7829721 http://www.cnblogs ...

  2. 期待微软平台即服务技术Service Fabric 开源

    微软的Azure Service Fabric的官方博客在3.24日发布了一篇博客 Service Fabric .NET SDK goes open source ,介绍了社区呼声最高的Servic ...

  3. 受限玻尔兹曼机(RBM)原理总结

    在前面我们讲到了深度学习的两类神经网络模型的原理,第一类是前向的神经网络,即DNN和CNN.第二类是有反馈的神经网络,即RNN和LSTM.今天我们就总结下深度学习里的第三类神经网络模型:玻尔兹曼机.主 ...

  4. Unity 3D Framework Designing(5)——ViewModel之间如何共享数据

    对于客户端应用程序而言,单页应用程序(Single Page Application)是最常见的表现形式.有经验的开发人员往往会把一个View分解多个SubView.那么,如何在多个SubView之间 ...

  5. zabbix 监控 tomcat jmx

    zabbix_server: zabbix_server.conf : # Add JavaGateway=127.0.0.1 JavaGatewayPort=10052 StartJavaPolle ...

  6. flume日志采集框架使用

    flume日志采集框架使用 本次学习使用的全部过程均不在集群上,均在本机环境,供学习参考 先决条件: flume-ng-1.6.0-cdh5.8.3.tar  去cloudrea下载flume框架,笔 ...

  7. Ubuntu离线安装VSCode(附带前期准备工作)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 在说正式步骤前先把准备工作做到位: 1.IP设置,这个因为是GUI的,手动设置 ...

  8. This Handler class should be static or leaks might occur(null) 解决办法 (转)

    原文链接:http://blog.csdn.net/wuleihenbang/article/details/17126371 首先解释下这句话This Handler class should be ...

  9. hibernate的反转引擎生成两个实体类的问题

    在使用myeclipse中自带的hibernate 进行jsp开发时候遇到了这个问题.使用hibernate的反转引擎从数据库生成生成实体类,一个表生成了两个类,xx.java和xxId.java . ...

  10. 微信小程序登录数据解密以及状态维持

    学习过小程序的朋友应该知道,在小程序中是不支持cookie的,借助小程序中的缓存我们也可以存储一些信息,但是对于一些比较重要的信息,我们需要通过登录状态维持来保存,同时,为了安全起见,用户的敏感信息, ...