- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 WCF 工作,正在编写基于 IHttpModule
的身份验证管理器,它运行良好。我的 Authentication
类中的一个方法在 Context.User
中创建了一个 GenericPrincipal
对象。
例如
app.Context.User = new GenericPrincipal(new GenericIdentity("Scott"), new string[] { "read" });
在 Service
中的一种方法中,我想分配给用户 PrincipalPermissionAttribute
但我不知道它应该如何工作,但它总是抛出一个 SecurityException
。例如:
[WebGet(UriTemplate = "/", RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml)]
[PrincipalPermission(SecurityAction.Demand, Role="read")] // it throw SecurityException
public SampleItem GetCollection()
{
bool user = HttpContext.Current.User.IsInRole("read"); // return true
bool user1 = HttpContext.Current.User.IsInRole("write"); // return false
return SampleItem.GetSampleItem();
}
也许 PrincipalPremissionAttribute
不使用 Context.Current.User
?但如果不是,那又怎样?
我试着解决了这个问题,做了一个非常简单的属性
[AttributeUsage(AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
public class MyAuthorizationAttribute : Attribute
{
public MyAuthorizationAttribute(params string[] roles)
{
foreach (string item in roles)
{
if(HttpContext.Current.User.IsInRole(item) == false)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = 401;
HttpContext.Current.Response.AddHeader("WWW-Authenticate", "Basic Realm");
HttpContext.Current.Response.StatusDescription = "Access Denied";
HttpContext.Current.Response.Write("401 Access Denied");
HttpContext.Current.Response.End();
}
}
}
}
但是应用程序不能使用这个。我的意思是,当我在 MyAttribute
构造函数上设置断点时,编译器不会在断点处停止,它看不到它。
[WebGet(UriTemplate = "/", RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml)]
[MyAuthorization("read")]
public SampleItem GetCollection()
{
bool user = HttpContext.Current.User.IsInRole("read"); // return true
bool user1 = HttpContext.Current.User.IsInRole("write"); // return false
return SampleItem.GetSampleItem();
}
最佳答案
对于 WCF,您需要通过 very specific mechanism 关联自定义主体,效果很好。另请注意,属性通常不会导致代码执行,并且只有在通过反射显式完成时才会被调用(除非您使用的是 PostSharp)。您不能只添加一个属性并让它自动执行操作。 MVC 等给人印象,但 MVC 中有代码检查属性并手动执行它们。
关于c# - 身份验证/PrincipalPermission 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5135770/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!