【发布时间】:2016-12-05 06:03:28
【问题描述】:
我编写了一个 CLR 存储过程来执行作为参数传递给它的命令。这是 CLR 存储过程的代码;我已在 SQL Server 中将程序集注册为“不安全”,并从中创建了存储过程。
[SqlProcedure]
public static int ExecuteCommand(string cmd)
{
int success = 0;
try
{
EventLog.WriteEntry("MyAppName", "Starting execution " + cmd + " Username: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name + " OR " + Environment.UserName, EventLogEntryType.Information);
Process p = new Process();
p.StartInfo = new ProcessStartInfo() { FileName = "cmd.exe", Arguments = cmd, WindowStyle = ProcessWindowStyle.Hidden};
p.Start();
}
catch (Exception ex)
{
EventLog.WriteEntry("MyAppName", "Executed command : " + cmd + " with error : " + ex.Message, EventLogEntryType.Error);
success = 1;
}
return success;
}
此代码能够写入事件查看器并将用户名打印为NT AUTHORITY\SYSTEM。然后它启动cmd.exe,但它不执行传递的命令(例如mkdir)并且cmd.exe 保持在运行状态。在任务管理器中,每次运行存储过程时,我都可以看到多个 cmd.exe 实例,但用户名列中没有值。 SQL Server 服务正在使用本地系统帐户运行。
这里出了什么问题?如果与权限有关,那么有没有办法在调用存储过程的用户的上下文下执行CLR存储过程?
【问题讨论】:
标签: .net sql-server sqlclr