asp.net – NancyFx和Windows身份验证
发布时间:2020-12-16 00:41:26 所属栏目:asp.Net 来源:网络整理
导读:我想使用NancyFx作为Intranet网络应用程序。所有的文档和论坛只提及表单和基本身份验证。任何人成功使用Nancy与Windows身份验证? 还有一些名为Nancy.Authentication.Stateless的东西,但我看不到这样做(看起来像在Apis中使用)。 解决方法 我最近在一个内部
我想使用NancyFx作为Intranet网络应用程序。所有的文档和论坛只提及表单和基本身份验证。任何人成功使用Nancy与Windows身份验证?
还有一些名为Nancy.Authentication.Stateless的东西,但我看不到这样做(看起来像在Apis中使用)。 解决方法
我最近在一个内部项目中使用了这个项目 – 我不太喜欢它,它将它与asp.net主机联系起来,但它完成了这项工作:
namespace Blah.App.Security { using System; using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Web; using Nancy; public static class SecurityExtensions { public static string CurrentUser { get { return GetIdentity().Identity.Name; } } public static bool HasRoles(params string[] roles) { if (HttpContext.Current != null && HttpContext.Current.Request.IsLocal) { return true; } var identity = GetIdentity(); return !roles.Any(role => !identity.IsInRole(role)); } public static void RequiresWindowsAuthentication(this NancyModule module) { if (HttpContext.Current != null && HttpContext.Current.Request.IsLocal) { return; } module.Before.AddItemToEndOfPipeline( new PipelineItem<Func<NancyContext,Response>>( "RequiresWindowsAuthentication",ctx => { var identity = GetIdentity(); if (identity == null || !identity.Identity.IsAuthenticated) { return HttpStatusCode.Forbidden; } return null; })); } public static void RequiresWindowsRoles(this NancyModule module,params string[] roles) { if (HttpContext.Current != null && HttpContext.Current.Request.IsLocal) { return; } module.RequiresWindowsAuthentication(); module.Before.AddItemToEndOfPipeline(new PipelineItem<Func<NancyContext,Response>>("RequiresWindowsRoles",GetCheckRolesFunction(roles))); } private static Func<NancyContext,Response> GetCheckRolesFunction(IEnumerable<string> roles) { return ctx => { var identity = GetIdentity(); if (roles.Any(role => !identity.IsInRole(role))) { return HttpStatusCode.Forbidden; } return null; }; } private static IPrincipal GetIdentity() { if (System.Web.HttpContext.Current != null) { return System.Web.HttpContext.Current.User; } return new WindowsPrincipal(WindowsIdentity.GetCurrent()); } public static Func<NancyContext,Response> RequireGroupForEdit(string group) { return ctx => { if (ctx.Request.Method == "GET") { return null; } return HasRoles(group) ? null : (Response)HttpStatusCode.Forbidden; }; } } } 它绕过所有安全检查,如果它来自本地(用于测试),这可能是一个坏主意,但它是防火墙后面的一个问题,所以这不是一个问题。 不建议你逐字地使用它,但可能指向正确的方向:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – MVC脚手架 – 参考程序集中缺少类
- asp.net – Oracle.ManagedDataAccess:TNS:无法解析指定的
- asp.net-web-api – ASP.NET Web API HTTP删除405方法不允许
- ASP.NET -- WebForm -- 给图片添加水印标记
- 如何使用ASP.NET创建XML文件并提示下载?
- asp.net-mvc – 在将站点托管到IIS之后,AngularJS路由无法正
- asp.net-mvc – 为什么DisplayFormat DataFormatString不工
- asp.net – 用户控制验证组问题
- asp.net – 从Web页面录制音频
- asp.net – dbo.aspnet_Users和dbo.aspnetUsers之间有什么区
推荐文章
站长推荐
- asp.net-web-api – 批量发布实体到WebAPI?
- 基于Azure IoT开发.NET物联网应用系列-全新的Azu
- asp.net-mvc – 如何下载Razor View引擎
- asp.net-mvc – ASP.NET MVC 3 – 这个布尔值如何
- asp.net中.aspx页面中各种符号的含义
- asp.net-mvc – T4MVC基本控制器没有默认构造函数
- asp.net-mvc – 任何支持ASP.NET MVC的图表控件,
- asp.net – 授权问题 – 匿名用户无法访问.jpeg或
- ASP MVC-3:在发布帖子后更新AJAX表单数据时出现
- asp.net-mvc – 如何限制访问我的服务器上的pdf文
热点阅读