这个安装时很简单的 只要 Install-Package Nancy.Hosting.Aspnet 就行了。

需要注意的是,千万不要用那个模板安装,通过创建nancyfx类型项目的方式安装是有问题的。原因是那个是很老的东西,装上后,用的是0.23版本的dll,而且配置文件(wenconfig)也不一样。  创建的项目,不要选网站类型,尽量选择c#类库项目,项目图标有绿色小球那种。

安装完成后,也可以安装其他的插件,比如Install-Package Nancy.Authentication.Stateless等,这里也是提醒下,看下插件的日期,尽量选择新版本的,2年前的插件未必在新的版本中能用。

Code Snippet
  1. <configuration>
  2. <configSections>
  3. <sectionname="nancyFx"type="Nancy.Hosting.Aspnet.NancyFxSection" />
  4. </configSections>
  5. <nancyFx>
  6. <!-- We can override the bootstrapper inside the config if we don't want to rely on the bootstrapper locator. -->
  7. <bootstrapperassembly="lxl"type="lxl.StatelessAuthBootstrapper" />
  8. </nancyFx>
  9. <system.web>
  10. <compilationdebug="true"targetFramework="4.0" />
  11. <httpRuntime />
  12. <httpHandlers>
  13. <addverb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
  14. </httpHandlers>
  15. </system.web>
  16. <system.webServer>
  17. <validationvalidateIntegratedModeConfiguration="false" />
  18. <modulesrunAllManagedModulesForAllRequests="true" />
  19. <httpErrorsexistingResponse="PassThrough" />
  20. <handlers>
  21. <addname="Nancy"verb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
  22. </handlers>
  23. </system.webServer>
  24. </configuration>

webconfig的配置类似上面

<bootstrapper assembly="lxl" type="lxl.StatelessAuthBootstrapper" />  表示默认加载的启动器。配置里可以没有,不是必须的。

<httpErrors existingResponse="PassThrough" />  这个需要注意下,这是处理http错误的方式,如果有这个配置,那么会用nancy自己的方法处理错误,默认返回的是状态码。

如果不加这个,那么会使用原始的错误处理方式。下面看个示例

public SecureModule()
       {
           this.RequiresAuthentication();

在你的module中加入验证,如果是用PassThrough这个配置,没有验证的情况下返回的是一个401的空白错误页。  去掉PassThrough,看到的是系统的错误页,或者要求你登陆的窗口。

从这里看出来,RequiresAuthentication失败,默认做网站级别的未登陆处理,而不是我们平时以为的业务级别的验证。

下面是验证是否的方法,是通过判断用户名来判断的。

public static bool IsAuthenticated(this IUserIdentity user)
       {
           return
               user != null
               && !String.IsNullOrWhiteSpace(user.UserName);
       }

再看看module是怎么验证的

Code Snippet
  1. public SecureModule()
  2. {
  3. this.RequiresAuthentication();
  4. Get["secure"] = x =>
  5. {
  6. //Context.CurrentUser was set by StatelessAuthentication earlier in the pipeline
  7. var identity = (DemoUserIdentity)this.Context.CurrentUser;
  8. //return the secure information in a json response
  9. var userModel = newUserModel(identity.UserName);
  10. returnthis.Response.AsJson(new
  11. {
  12. SecureContent = "here's some secure content that you can only see if you provide a correct apiKey",
  13. User = userModel
  14. });
  15. };
  16. }

首先看看用户是否登陆,这个this.RequiresAuthentication();可以加到最外面 ,就是对module验证。也可放到 Get["secure"] = x =>这个action里面,那么就是对action验证。

var identity = (DemoUserIdentity)this.Context.CurrentUser; 是获取用户的登陆信息,是可以自定义的。可以从token cookie等方式获取。

然后通过判断identity 的属性 比如角色id之类的进行业务逻辑的验证。

标记: nancy,net

nancyfx的安装笔记的更多相关文章

  1. MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记

    MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记 说明 以root账户登录Linux操作系统,注意:本文中的所有命令行前面的 #> 表示命令行提示符 ...

  2. 基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记

    基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记 飞翔的蜘蛛人 注1:本人新手,文章中不准确的地方,欢迎批评指正 注2:知识储备应达到Linux入门级水平 ...

  3. sublime 安装笔记

    sublime 安装笔记 下载地址 安装package control 根据版本复制相应的代码到console,运行 按要求重启几次后再按crtl+shift+p打开命令窗口 输入pcip即可开始安装 ...

  4. docker在ubuntu14.04下的安装笔记

    本文主要是参考官网教程进行ubuntu14.04的安装. 下面是我的安装笔记. 笔记原件完整下载: 链接: https://pan.baidu.com/s/1dEPQ8mP 密码: gq2p

  5. ArchLinux 安装笔记:续 --zz

    续前话 在虚拟机里调试了几天,终于鼓起勇气往实体机安装了,到桌面环境为止的安装过程可以看我的前一篇文章<ArchLinux 安装笔记>.桌面环境我使用的是 GNOME,虽然用了很长一段时间 ...

  6. Hadoop1.x与2.x安装笔记

    Hadoop1.x与2.x安装笔记 Email: chujiaqiang229@163.com 2015-05-09 Hadoop 1.x 安装 Hadoop1.x 集群规划 No 名称 内容 备注 ...

  7. PHP7安装笔记

    PHP7安装笔记 时间 -- :: 喵了个咪 原文 http://www.hdj.me/php7-install-note 主题 PHP # 安装mcrypt yum install -y php-m ...

  8. python 库安装笔记

    python 库安装笔记 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-2-22 友情提示 安装python库的过程中 ...

  9. 开始使用gentoo linux——gentoo安装笔记(下)

    gentoo安装笔记(下) 上一章,已经对操作系统安装做了充分准备,并且已经从livecd(u盘系统)切换进入了gentoo安装环境中. 不过现在才是真正的开始!打起精神!这可不是在装ubuntu! ...

随机推荐

  1. Boost C++ 库 中文教程(全)

    Boost C++ 库 目录 第 1 章 简介 第 2 章 智能指针 第 3 章 函数对象 第 4 章 事件处理 第 5 章 字符串处理 第 6 章 多线程 第 7 章 异步输入输出 第 8 章 进程 ...

  2. jquery textSearch实现页面搜索 注意!!!!调用这个插件后,js事件绑定如,on、bind、live delegate全部失效,折腾了我一整天!!!

    今天我们介绍的这个插件来着http://www.zhangxinxu.com/wordpress/,张鑫旭的文章写得挺好的,大家有兴趣的多看看. 我们今天的这个插件叫“jquery.textSearc ...

  3. Oracle 取某100天的每一天的日期

    SELECT TO_DATE('2016-01-01', 'yyyy-MM-dd') + ROWNUM - 1 as daylist,TO_DATE('2016-01-01', 'yyyy-MM-dd ...

  4. Autofac Property Injection and Method Injection

    While constructor parameter injection is the preferred method of passing values to a component being ...

  5. 公共文件模块include

    首先新建一个include 把所有引入的文件放入公共文件里 function getBaseURL() { var pathName = window.document.location.pathna ...

  6. Nginx 前后端分离配置 分发

    前端项目VUE  端口8081 , 后端项目JAVA 端口8080 # For more information on configuration, see: # * Official English ...

  7. 【C】常用的字符串函数

    1. strcpy 函数名:strcpy 用法:char *strcpy(char *destin, char *cource) 功能:将一个字符串从一个拷贝到另外一个 程序示例: #include ...

  8. python3菜鸟教程

    http://www.runoob.com/python3/python3-class.html

  9. Python小程序之「读取站点地图 自动为Gitalk创建Issues」

    首发于个人博客 想获得更好的阅读体验,烦请移步⬆️ 前言 前些天给博客加了评论功能,试了Disqus.Valine等一干评论系统,最后还是选择了在大陆相对友好而且符合技术博客风格的Gitalk.但是由 ...

  10. 5-math中函数汇总

    math.h 数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有:1 三角函数double sin (double);double cos (double);double tan ( ...