NancyFx Authentication per Route

淺唱寂寞╮ 提交于 2019-12-06 06:31:34

问题


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?


回答1:


I had the same problem. However it turns out the RequiresAuthentication works at both the module level and the route level. To demonstrate, here is some code ripped out my current project (not all routes shown for brevity).

public class RegisterModule : _BaseModule
{
    public RegisterModule() : base("/register")
    {
        Get["/basic-details"] = _ => View["RegisterBasicDetailsView", Model];

        Get["/select"] = _ =>
            {
                this.RequiresAuthentication();
                return View["RegisterSelectView", Model];
            };
    }
}

Of course the only problem with doing it this way is that all the protected routes in the module need to call RequiresAuthentication. In the case of my module above, I have another 5 routes (not shown) all of which need protecting, so that makes six calls to RequiresAuthentication instead of one at the module level. The alternative would be to pull the unprotected route into another module, but my judgement was that a proliferation of modules is worse than the additional RequiresAuthentication calls.




回答2:


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

    }
}



回答3:


I ran into the same issue, here's how I solved it.

        var module = new MyModule();
        module.AddBeforeHookOrExecute(context => null, "Requires Authentication");
        _browser = new Browser(with =>
            {
                with.Module(module);
                with.RequestStartup((container, pipelines, ctx) =>
                    {
                        ctx.CurrentUser = new User { UserId = "1234", UserName = "test"};
                    });
            });

I can now use this.RequiresAuthentication() at the module level and run my unit tests.



来源:https://stackoverflow.com/questions/12185257/nancyfx-authentication-per-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!