zoukankan      html  css  js  c++  java
  • nancyfx的安装笔记

    这个安装时很简单的 只要 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.  
    5.   </configSections>
    6.   <nancyFx>
    7.     <!-- We can override the bootstrapper inside the config if we don't want to rely on the bootstrapper locator. -->
    8.     <bootstrapperassembly="lxl"type="lxl.StatelessAuthBootstrapper" />
    9.   </nancyFx>
    10.   <system.web>
    11.     <compilationdebug="true"targetFramework="4.0" />
    12.     <httpRuntime />
    13.     <httpHandlers>
    14.       <addverb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
    15.     </httpHandlers>
    16.  
    17.   </system.web>
    18.   <system.webServer>
    19.     <validationvalidateIntegratedModeConfiguration="false" />
    20.     <modulesrunAllManagedModulesForAllRequests="true" />
    21.     <httpErrorsexistingResponse="PassThrough" />
    22.     <handlers>
    23.       <addname="Nancy"verb="*"type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler"path="*" />
    24.     </handlers>
    25.  
    26.   </system.webServer>
    27.  
    28. </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.  
    5.             Get["secure"] = x =>
    6.                 {
    7.                     //Context.CurrentUser was set by StatelessAuthentication earlier in the pipeline
    8.                     var identity = (DemoUserIdentity)this.Context.CurrentUser;
    9.  
    10.                     //return the secure information in a json response
    11.                     var userModel = newUserModel(identity.UserName);
    12.                     returnthis.Response.AsJson(new
    13.                         {
    14.                             SecureContent = "here's some secure content that you can only see if you provide a correct apiKey",
    15.                             User = userModel
    16.                         });
    17.                 };
    18.         }

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

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

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

    标记: ,
  • 相关阅读:
    Jetty容器集群配置Session存储到MySQL、MongoDB
    js清除浏览器缓存的几种方法
    Maven学习 (四) 使用Nexus搭建Maven私服
    ActiveMQ入门实例(转)
    SOAP Webservice和RESTful Webservice
    Redis集群搭建与简单使用
    如何设置SVN提交时强制添加注释
    linux下vi命令大全
    锦隆驾校考试场---大路
    锦隆驾校考试场---小路
  • 原文地址:https://www.cnblogs.com/wang2650/p/4853368.html
Copyright © 2011-2022 走看看