2012-01-30 131 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