每路线NancyFx认证 [英] NancyFx Authentication per Route

查看:242
本文介绍了每路线NancyFx认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在源$ C ​​$ C RequiresAuthentication看到()确实为整个模块的验证检查。有没有什么办法,每路做到这一点?


解决方案

 命名空间Kallist.Modules {    #区域命名空间    使用系统;
    使用南希;    #endregion    公共静态类ModuleExtensions {        #区域方法        公共静态响应WithAuthentication(这NancyModule模块,Func键<应变及GT; executeAuthenticated){
            如果((module.Context.CurrentUser = NULL)及!&安培;!string.IsNullOrWhiteSpace(module.Context.CurrentUser.UserName)){
                返回executeAuthenticated();
            }
            返回新的响应{状态code =的HTTPStatus code.Unauthorized};
        }        #endregion    }
}

From what I saw in the source code RequiresAuthentication() does an Authentication check for the whole module. Is there any way to do this per Route?

解决方案

namespace Kallist.Modules {

    #region Namespaces

    using System;
    using Nancy;

    #endregion

    public static class ModuleExtensions {

        #region Methods

        public static Response WithAuthentication(this NancyModule module, Func<Response> executeAuthenticated) {
            if ((module.Context.CurrentUser != null) && !string.IsNullOrWhiteSpace(module.Context.CurrentUser.UserName)) {
                return executeAuthenticated();
            }
            return new Response { StatusCode = HttpStatusCode.Unauthorized };
        }

        #endregion

    }
}

这篇关于每路线NancyFx认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆