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如下图

运行如下图



谢谢观看!
NancyFx 2.0的开源框架的使用-Authentication的更多相关文章
- NancyFx 2.0的开源框架的使用-Basic
这是NancyFx开源框架中的Basic认证,学习一下! 首先当然是新建一个空的Web,BasicDemo 继续在项目中添加Nuget包,记得安装的Nuget包是最新的预发行版 Nancy Nancy ...
- NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)
NancyFx框架的自定义模块 新建一个空的Web项目 然后通过NuGet库安装下面的包 Nancy Nancy.Hosting.Aspnet 然后添加Models,Module,Views三个文件夹 ...
- NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)
NancyFx框架中使用绑定模型 新建一个空的Web程序 然后安装Nuget库里面的包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Spark 并在We ...
- NancyFx 2.0的开源框架的使用-HosingOwin
Nancy框架的Owin使用 先建一个空的Web项目 然后往Nuget库里面添加Nancy包 Nancy Nancy.Owin Nancy.ViewEnglines.Spark 然后添加Models, ...
- NancyFx 2.0的开源框架的使用-Forms
同样的像前面2篇博文一样,每个项目的开始基本都是建个空的Web项目 在NuGet库中安装以下几个NuGet包 Nancy Nancy.Authentication.Forms Nancy.Hostin ...
- NancyFx 2.0的开源框架的使用-Stateless
同样和前面一样新建一个空的Web项目,都在根目录添加Module,Models,Views文件夹 添加Nuget包 在Models文件夹里面添加UserModel类 public string Use ...
- NancyFx 2.0的开源框架的使用-Stateless(二)
继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...
- NancyFx 2.0的开源框架的使用-AspnetBootstrapping
新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...
- NancyFx 2.0的开源框架的使用-Caching
新建一个空的Web项目,命名CachingDemo 然后添加三个Nuget安装包 Nancy Nancy.Hosting.Aspnet Nancy.ViewsEngines.Razor 然后往项目里面 ...
随机推荐
- 细心!SQL语句进行运算时使用字符串时缺失精度的细节!
昨天没有更新,特此来说明下原因,昨天回到家时已经甚晚,正逢公司这几天项目比较紧张(bug多,赶需求,看着bug单齐刷刷的转过来,心都颤抖了一下),没有及时准备素材,今天又加了一天班(现在还在公司,偷个 ...
- 为什么使用 Containjs 模块化管理工具效率高?
为什么使用 Containjs 模块化管理工具效率高? 要说明这个首先得说明一下,Containjs 的模块加载原理. 第一步,首先使用异步加载(ajax)在 js 目录下的 app.js 入口模块( ...
- 老李分享: Oracle Performance Tuning Overview 翻译
老李分享: Oracle Performance Tuning Overview 翻译 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工 ...
- 深入学习 DUBBO
1.什么是 RPC 协议? RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显 ...
- java中的==、equals()、hashCode()源码分析
转载自:http://www.cnblogs.com/xudong-bupt/p/3960177.html 在Java编程或者面试中经常会遇到 == .equals()的比较.自己看了看源码,结合实际 ...
- ViewPager—01引导页的制作
布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 在my.ini文件中配置mysql统一字符集
测试的mysql版本为:5.7.14 查看mysql字符集命令: show variables like 'character_set_%'; 以下是在my.ini文件中配置mysql统一字符集参数: ...
- firefox上安装selenium ide失败
Selenium 初学者第一步: 最近在学习selenium,但是在安装的时候遇到了问题.我是直接在firefox安装的Selenium IDE ,虽然下载安装之后存在于扩展中,但是工具栏里并没有显示 ...
- weex里Vuex state使用storage持久化
在weex里使用Vuex作为state管理工具,问题来了,如何使得state可以持久化呢?weex官方提供store模块,因此我们可以尝试使用该模块来持久化state. 先看下该模块介绍: stora ...
- ios animation 动画效果实现
1.过渡动画 CATransition CATransition *animation = [CATransition animation]; [animation setDuration:1.0]; ...