2012-01-30 79 views
0

最近我创建了一个应用程序,它可以获取用户的密码,其用户名作为参数提供,我检查了代码,它的工作正常,现在我创建了一个类库,以便我可以添加这在SQL Server 2008中声明,下面是我的类库的代码。Sql-Clr安全异常

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Security; 

public class DnnUserCredentials 
{ 
    public static string GetUserPassword(string username) 
    { 
     MembershipUser objUser = Membership.GetUser(username); 
     return objUser.GetPassword(); 
    } 
} 

我创建SQL Server 2008中的组件类似下面

CREATE ASSEMBLY DNN_USER_CREDENTIALS FROM 'E:\NBM SITES\SqlProjectDll (DO NOT DELETE)\UserCredentials.dll' WITH PERMISSION_SET = SAFE 

我得到的命令成功的消息完成了,然后我创建像下面的函数:

ALTER FUNCTION [dbo].[fn_UserCredentials](@UserName [nvarchar](max)) 
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER 
AS 
EXTERNAL NAME [DNN_USER_CREDENTIALS].[DnnUserCredentials].[GetUserPassword] 

这里过我命令完成成功消息,现在当我试图调用函数使用

SELECT [dbo].[fn_UserCredentials]('host') 

它抛出我下面的错误:

A .NET Framework error occurred during execution of user-defined routine or aggregate "fn_UserCredentials": 
System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 
System.Security.SecurityException: 
    at DnnUserCredentials.GetUserPassword(String username) 

任何解决方案。

回答

0

不知道可能是什么问题。首先尝试将PERMISSION_SET更改为Unrestricted。 当这无助于尝试从Visual Studio调试DLL。

0

您需要设置PERMISSION_SET = UNSAFE才能访问远程资源。 此外,您可能还想将TRUSTWORTHY标志设置为ON。

ALTER [<DBName>] SET TRUSTWORTHY ON 
GO 
CREATE ASSEMBLY [<assemblyName>] 
--user with sysadmin rights for this DB may be required for deploing 
AUTHORIZATION [<someSysadmin>] 
FROM '[<pathToDll>]' 
--compiled with unsafe to access EventLog for example 
WITH PERMISSION_SET = UNSAFE 
GO