简体   繁体   English

SSRS使用自定义程序集检查用户是否在组中

[英]SSRS check if user in group using Custom Assembly

I have created a Custom Assembly for my SSRS Project. 我已经为我的SSRS项目创建了一个自定义程序集。

The Custom Assembly has got 2 functions, IsInGroup and MyTest : 自定义程序集具有两个函数IsInGroupMyTest

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;

namespace SSRS_Custom_Fuctions
{
    public class Class1
    {

        public static bool IsInGroup(string user, string group)
        {
            using (var identity = new WindowsIdentity(user))
            {
                var principal = new WindowsPrincipal(identity);
                return principal.IsInRole(group);
            }
        }

        public static string MyTest()
        {
            return "Hello World";
        }
    }
}

1) The basic function MyTest which returns a string 'Hello World' works perfectly fine from the Report using the expression =SSRS_Custom_Functions.Class1.MyTest() 1)返回表达式'Hello World'的基本函数MyTest使用=SSRS_Custom_Functions.Class1.MyTest()表达式从Report可以正常工作

2) The function IsInGroup which returns a boolean value is not working. 2)返回布尔值的函数IsInGroup不起作用。 This is using the System.Security.Principal namespace to check if the username passed to the function exists in the group passed to the function. 这使用System.Security.Principal命名空间检查传递给函数的用户名是否存在于传递给函数的组中。 When trying to invoke it using expression =SSRS_Custom_Functions.Class1.IsInGroup(User.User1, "MyGroupName") , the report is bailing out with the following error message: 当尝试使用表达式=SSRS_Custom_Functions.Class1.IsInGroup(User.User1, "MyGroupName")调用它时,报告将出现以下错误消息:

request for the permission of type System.Security failed 请求类型System.Security的许可失败

I have modified the configuration files rssrvpolicy.config in the ReportingServices file path and RSPreviewPolicy.config in the the VisualStudio file path according to Microsoft KB920769 . 我已经修改了配置文件rssrvpolicy.config在ReportingServices文件路径和RSPreviewPolicy.config根据在该VisualStudio的文件路径微软KB920769

I added a CodeGroup which gives FullTrust to my custom assembly. 我添加了一个CodeGroup ,它使FullTrust进入我的自定义程序集。

The following has been added to the policy level element: 以下已添加到策略级别元素:

<CodeGroup class="UnionCodeGroup"
           version="1"
           PermissionSetName="FullTrust"
           Name="SSRS_Custom_Fuctions"
           Description="Code group for my data processing extension">

<IMembershipCondition class="UrlMembershipCondition"
                      version="1"
                      Url="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\SSRS_Custom_Fuctions.dll"/>
</CodeGroup>

I am still getting the same error message as above. 我仍然收到与上述相同的错误消息。

In your assembly, you need to Assert the SecurityPermission object first before using it. 在您的程序集中,您需要先声明SecurityPermission对象,然后再使用它。

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;

namespace SSRS_Custom_Fuctions
{
    public class Class1
    {

        public static bool IsInGroup(string user, string group)
        {

        System.Security.Permissions.SecurityPermission sp = new System.Security.Permissions.SecurityPermission(System.Security.Permissions.PermissionState.Unrestricted);
        sp.Assert();

            using (var identity = new WindowsIdentity(user))
            {
                var principal = new WindowsPrincipal(identity);
                return principal.IsInRole(group);
            }
        }

        public static string MyTest()
        {
            return "Hello World";
        }
    }
}

In the AssemblyInfo file you need to add namespace System.Security and 在AssemblyInfo文件中,您需要添加namespace System.Security

[assembly: AllowPartiallyTrustedCallers()] .

Sign the assembly with certificate and also deploy the same in GAC of machine. 用证书签名组件,并将其部署在机器的GAC中。

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

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