Sql-Clr 安全异常
最近我创建了一个可以获取用户密码的应用程序,其用户名作为参数提供,我检查了代码,它工作正常,现在我创建了一个类库,以便我可以在下面的sql server 2008中添加这个程序集是我的类库的代码。
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)
任何解决方案。
Recently i created an application which could get the password of the user, whose username is provided as the parameter, i checked the code, its working fine, now i created a class library so that i can add this assemnbly in sql server 2008, below is the code of my class library.
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();
}
}
i created the assembly in sql server 2008 like below
CREATE ASSEMBLY DNN_USER_CREDENTIALS FROM 'E:\NBM SITES\SqlProjectDll (DO NOT DELETE)\UserCredentials.dll' WITH PERMISSION_SET = SAFE
i got the command completed successfully message, then i created a function like below:
ALTER FUNCTION [dbo].[fn_UserCredentials](@UserName [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DNN_USER_CREDENTIALS].[DnnUserCredentials].[GetUserPassword]
here too i got the command completed successfully message, now when i tried to call the function using
SELECT [dbo].[fn_UserCredentials]('host')
it throws me below error:
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)
any solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定可能是什么问题。首先尝试将 PERMISSION_SET 更改为 Unrestricted。
如果这没有帮助,请尝试从 Visual Studio 调试 DLL。
Not sure what can be the problem. First try to change the PERMISSION_SET to Unrestricted.
When this does not help try to debug the DLL from visual studio.
您需要设置PERMISSION_SET = UNSAFE才能访问远程资源。
此外,您可能希望将 TRUSTWORTHY 标志设置为 ON。
You need to set PERMISSION_SET = UNSAFE to get access to remote resources.
Also you might want to set TRUSTWORTHY flag to ON.