nancyfx的安装笔记
这个安装时很简单的 只要 Install-Package Nancy.Hosting.Aspnet 就行了。
需要注意的是,千万不要用那个模板安装,通过创建nancyfx类型项目的方式安装是有问题的。原因是那个是很老的东西,装上后,用的是0.23版本的dll,而且配置文件(wenconfig)也不一样。 创建的项目,不要选网站类型,尽量选择c#类库项目,项目图标有绿色小球那种。
安装完成后,也可以安装其他的插件,比如Install-Package Nancy.Authentication.Stateless等,这里也是提醒下,看下插件的日期,尽量选择新版本的,2年前的插件未必在新的版本中能用。
- <configuration>
- <configSections>
- <sectionname="nancyFx"type="Nancy.Hosting.Aspnet.NancyFxSection" />
- </configSections>
- <nancyFx>
- <!-- We can override the bootstrapper inside the config if we don't want to rely on the bootstrapper locator. -->
- <bootstrapperassembly="lxl"type="lxl.StatelessAuthBootstrapper" />
- </nancyFx>
- <system.web>
- <compilationdebug="true"targetFramework="4.0" />
- <httpRuntime />
- <httpHandlers>
- <addverb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
- </httpHandlers>
- </system.web>
- <system.webServer>
- <validationvalidateIntegratedModeConfiguration="false" />
- <modulesrunAllManagedModulesForAllRequests="true" />
- <httpErrorsexistingResponse="PassThrough" />
- <handlers>
- <addname="Nancy"verb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
- </handlers>
- </system.webServer>
- </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是怎么验证的
- public SecureModule()
- {
- this.RequiresAuthentication();
- Get["secure"] = x =>
- {
- //Context.CurrentUser was set by StatelessAuthentication earlier in the pipeline
- var identity = (DemoUserIdentity)this.Context.CurrentUser;
- //return the secure information in a json response
- var userModel = newUserModel(identity.UserName);
- returnthis.Response.AsJson(new
- {
- SecureContent = "here's some secure content that you can only see if you provide a correct apiKey",
- User = userModel
- });
- };
- }
首先看看用户是否登陆,这个this.RequiresAuthentication();可以加到最外面 ,就是对module验证。也可放到 Get["secure"] = x =>这个action里面,那么就是对action验证。
var identity = (DemoUserIdentity)this.Context.CurrentUser; 是获取用户的登陆信息,是可以自定义的。可以从token cookie等方式获取。
然后通过判断identity 的属性 比如角色id之类的进行业务逻辑的验证。
nancyfx的安装笔记的更多相关文章
- 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操作系统,注意:本文中的所有命令行前面的 #> 表示命令行提示符 ...
- 基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记
基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记 飞翔的蜘蛛人 注1:本人新手,文章中不准确的地方,欢迎批评指正 注2:知识储备应达到Linux入门级水平 ...
- sublime 安装笔记
sublime 安装笔记 下载地址 安装package control 根据版本复制相应的代码到console,运行 按要求重启几次后再按crtl+shift+p打开命令窗口 输入pcip即可开始安装 ...
- docker在ubuntu14.04下的安装笔记
本文主要是参考官网教程进行ubuntu14.04的安装. 下面是我的安装笔记. 笔记原件完整下载: 链接: https://pan.baidu.com/s/1dEPQ8mP 密码: gq2p
- ArchLinux 安装笔记:续 --zz
续前话 在虚拟机里调试了几天,终于鼓起勇气往实体机安装了,到桌面环境为止的安装过程可以看我的前一篇文章<ArchLinux 安装笔记>.桌面环境我使用的是 GNOME,虽然用了很长一段时间 ...
- Hadoop1.x与2.x安装笔记
Hadoop1.x与2.x安装笔记 Email: chujiaqiang229@163.com 2015-05-09 Hadoop 1.x 安装 Hadoop1.x 集群规划 No 名称 内容 备注 ...
- PHP7安装笔记
PHP7安装笔记 时间 -- :: 喵了个咪 原文 http://www.hdj.me/php7-install-note 主题 PHP # 安装mcrypt yum install -y php-m ...
- python 库安装笔记
python 库安装笔记 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-2-22 友情提示 安装python库的过程中 ...
- 开始使用gentoo linux——gentoo安装笔记(下)
gentoo安装笔记(下) 上一章,已经对操作系统安装做了充分准备,并且已经从livecd(u盘系统)切换进入了gentoo安装环境中. 不过现在才是真正的开始!打起精神!这可不是在装ubuntu! ...
随机推荐
- struts2学习(1)struts2 helloWorld
一.struts2简介: 二.helloWorld: 1)工程结构: HelloWorldAction.java: package com.cy.action; import com.opensymp ...
- postman-3断言
用例3A原则: arrange:初始化测试对象 act:操作.接口测试中通过不同的参数调用接口. assert:断言 snippets提供了一些断言的使用方法. 在新版的postman中,test中方 ...
- 通过日志查看Web Api详细运行过程
1. 通过Nuget安装System.Web.Http.Tracing. 2. 通过HttpConfiguration,注册SystemDiagnosticsTraceWriter public st ...
- 精《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #5 使用checkpatch.pl检查补丁的格式
HACK #5 使用checkpatch.pl检查补丁的格式 本节介绍发布前检查补丁格式的方法.Linux内核是由多个开发者进行开发的.因此,为了保持补丁评估与源代码的可读性,按照统一的规则进行编写是 ...
- Solr聚合查询
1 分组查询 概述:Solr常用的分组查询有两种,Facet分组和Group分组,分别以下列出这两种查询: 1.1 Facet分组 solr种以导航为目的的查询结果成为facet,在用户 ...
- Linux安装Python虚拟环境virtualenv
1.安装virtulenv.virtulenvwrapper包 pip install virtualenv virtualenvwrapper 2.virtualenvwrapper是virtual ...
- 阿里云短信接口python版本
# -*- coding: utf-8 -*- #!/usr/bin/python #encoding=utf-8 import sys from aliyunsdkdysmsapi.request. ...
- leetcode890
public class Solution { public string ConvertString(string pattern) { var dic = new Dictionary<ch ...
- sql server2008本地连接选择windows身份验证无法登陆的解决办法
1.安装完sqlserver数据库,本地连接登录不了 解决办法:进入cmd,输入net start mssqlserver 服务启动后,再次用windows身份验证就可以登陆本地数据库了
- Memcpy, blockcopy的进一步理解
using System; using System.Runtime.InteropServices; using System.IO; namespace tx { struct ST { publ ...