Tag: iprincipal

代码忽略了PrincipalPermission属性?

我在所有具有PrincipalPermission属性的业务对象上都有一个Delete方法。 例: [PrincipalPermission(SecurityAction.Demand, Role = “Vendor Manager”)] public static bool Delete(Vendor myVendor) { //do work here } 问题是它似乎完全忽略了我的PrincipalPermission。 无论他们扮演什么样的角色,它都可以让任何人通过。 还有其他我忘了做的事吗? 我在Application Startup部分的应用程序的global.asax中添加了以下内容: AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal); 但这也没有任何区别。 我也尝试了以下内容: public static bool Delete(Vendor myVendor) { PrincipalPermission iPerm = new PrincipalPermission(null, “Vendor Manager”); iPerm.Demand(); //do work here } 并且不知道,这个工作得很好!….任何关于它为什么单向工作而不是另一个工作的想法?

如何在ASP.NET MVC中更轻松地访问我的自定义IPrincipal?

我编写了一个自定义主体对象,其中包含一些额外的字段(除了用户名之外的电子邮件和用户ID)。 为了访问这些属性,我必须将Context.User对象转换为我的自定义主体。 @Html.GetGravitarImage((User as CustomPrincipal).Email) 通过我的global.ascx中的Application_AuthenticateRequest创建/反序列化此自定义主体。 您可以在此处查看此问题以获取更多信息。 private void Application_AuthenticateRequest(Object source, EventArgs e) { var application = (HttpApplication)source; var context = application.Context; // Get the authentication cookie string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie = context.Request.Cookies[cookieName]; if (authCookie == null) return; var authTicket = FormsAuthentication.Decrypt(authCookie.Value); context.User = CustomPrincipal.CreatePrincipalFromCookieData(authTicket.UserData); } 但是,如果用户未经过身份validation,那么我对CustomPrincipal的强制转换将失败(因为它不会在上面的方法中注入),并且(User as CustomPrincipal)的结果将返回null,从而给我一个空引用上面我的方法尝试获取电子邮件时的exception。 什么是这个问题的干净解决方案? 我想让访问我的自定义主体变得容易,并且必须执行以下操作似乎很麻烦: @Html.GetGravitarIcon((User is […]

在unit testing中为ApiController设置User属性

我对ApiController的unit testing使用一些帮助器方法来实例化控制器: public static ResourcesController SetupResourcesController(HttpRequestMessage request, IResourceMetadataRepository repo, IUnitOfWorkService unitOfWorkService) { var config = new HttpConfiguration(); var defaultRoute = config.Routes.MapHttpRoute(RouteNames.DefaultApi , “api/{controller}/{id}”); var routeData = new HttpRouteData(defaultRoute, new HttpRouteValueDictionary { { “controller”, “resources” } }); var resourcesController = new ResourcesController(repo, unitOfWorkService) { ControllerContext = new HttpControllerContext(config, routeData, request), Request = request }; resourcesController.Request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, routeData); […]

使用自定义成员资格和角色提供程序在MVC中实现IPrincipal和IIdentity

我坚持使用自定义iprincpal和iidentity对象的实现。 我现在花了一天时间来搜索如何实现这些权利并用更多信息扩展它。 我想用自定义变量(如全名或其他)扩展Information @Context.User.Identity.Name 。 编辑 :现在我得到以下代码,但如果我尝试阅读@((CustomPrincipal)Context.User.Identity).Nachname我收到一个错误, System.Web.Security.FormsIdentity无法转换为CustomPrincipal 。 有任何想法吗? public class CustomPrincipal : GenericPrincipal { public CustomPrincipal(IIdentity identity, String[] roles) : base(identity, roles){ } public String Vorname { get; set; } public String Nachname { get; set; } } AccountModel: public class FormsAuthenticationService : IFormsAuthenticationService { public void SignIn(string userName, bool createPersistentCookie) { if […]

这个基本控制器ASP.NET MVC 3中的自定义主体是非常低效的吗?

尽管我已经在这里待了一段时间,但这是我第一个关于SO的问题,所以请温柔地对待我。 我正在使用ASP.NET MVC 3 ,我想创建一个自定义Principal这样我就可以存储有关当前用户的更多信息而不是标准,因此不必经常访问数据库。 这是我追求的相当标准的东西。 我们先说一下电子邮件地址和用户ID。 我决定将对象存储在缓存中,因为我知道不建议将它存储在会话中。 我也不想继续强制转换User对象,所以我想覆盖控制器中的User对象。 所以我可以去User.UserId并保证一些东西。 所以我创建了一个这样的自定义主体: public class MyPrincipal : IPrincipal { public MyPrincipal(IIdentity ident, List roles, string email, Guid userId) { this._identity = ident; this._roles = roles; this._email = email; this._userId = userId; } IIdentity _identity; public IIdentity Identity { get { return _identity; } } private List _roles; […]