简体   繁体   English

使用ClaimsPrincipalPermission和ADFS 2.0返回的声明注释类

[英]Annotating a class using ClaimsPrincipalPermission with a claim returned by ADFS 2.0

I am using a claims enable web app. 我正在使用启用声明的网络应用。 I want to lock down a web page with a custom attribute to allow access to the page if a certain claim(s) is present.. 我想用自定义属性锁定网页,以便在存在某些声明时允许访问该页面。

I could do the following 我可以执行以下操作

Claim claim = claimsIdentity.Claims.FirstOrDefault(c => c.ClaimType 
== "http://somedomain.com/claims/MemberOfGroup" && c.Value == "domain\\test group");

and redirect them away from the page if claim is null.. 并在版权声明为null时将其重定向到页面之外。

But how could I do this by annotating the class. 但是我该如何通过注释班级做到这一点。

I was looking at the ClaimsPrincipalPermission 我在看ClaimsPrincipalPermission

[ClaimsPrincipalPermission(SecurityAction.Demand, Resource="", Operation="")]

but for life of me can't see what to put into resource and operation. 但是我一生都看不到该投入资源和运营。 How do I get my claim that is returned by adfs into a claimsprincipalpermission . 我如何将我的由adfs返回的索赔归还给claimsprincipalpermission Do I need to write a lot of custom classes to do this? 为此,我需要编写很多自定义类吗?

Thanks, 谢谢,

You will have to create your own AuthorizationManager class that inherits from ClaimsAuthorizationManager and override the CheckAccess() method. 您将必须创建自己的AuthorizationManager类,该类从ClaimsAuthorizationManager继承并覆盖CheckAccess()方法。 This method will be called for any method decorated with the ClaimsPrincipalPermission attribute. 对于使用ClaimsPrincipalPermission属性修饰的任何方法,将调用此方法。 It will be passed an AuthorizationContext object that contains the Resource string, the Operation String and the IPrincipal Object that contains all the claims the current user has. 它将传递一个AuthorizationContext对象,该对象包含资源字符串,操作字符串和IPrincipal对象,该对象包含当前用户拥有的所有声明。 Inside this method you can parse this data any way you choose to determine access and return true or false. 在此方法内,您可以选择确定访问权限的任何方式解析此数据并返回true或false。

In the WIF SDK samples, you will find a simple, console app project called ClaimsAuthorization that demonstrates this. 在WIF SDK示例中,您将找到一个名为ClaimsAuthorization的简单控制台应用程序项目,以证明这一点。 It uses the resource and operation to look in the application config file to see what claims the user needs and returns true or false accordingly. 它使用资源和操作来查看应用程序配置文件,以查看用户要求的内容并相应地返回true或false。 Of course, you don't have to do that, you can use any other logic you choose. 当然,您不必这样做,可以使用选择的任何其他逻辑。

The sample is usually under C:\\Program Files (x86)\\Windows Identity Foundation SDK\\v4.0\\Samples\\Extensibility\\Claims based Authorization 该示例通常位于C:\\ Program Files(x86)\\ Windows Identity Foundation SDK \\ v4.0 \\ Samples \\ Extensibility \\ Claims基于授权

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM