【问题标题】:How to run xp_cmdshell from my app in ASP.NET MVC如何在 ASP.NET MVC 中从我的应用程序运行 xp_cmdshell
【发布时间】:2023-03-17 03:21:01
【问题描述】:

在我的应用程序中,我正在将数据读取到数据库,然后我想启动集成服务将数据导入多维数据集。这是我用来从 SSMS 启动它的命令:

execute master..xp_cmdshell 'dtexec /DTS "\MSDB\B_Costs" /SERVER . /CHECKPOINTING OFF /REPORTING V'

但是如何从我的应用程序中使用它?我在开始时遇到此错误:
对象“xp_cmdshell”、数据库“mssqlsystemresource”、架构“sys”的执行权限被拒绝。

使用这样的命令后:

GRANT EXECUTE ON xp_cmdshell TO [IIS APPPOOL\DefaultAppPool]

我遇到另一个错误:
xp_cmdshell 代理帐户信息无法检索或无效。验证“##xp_cmdshell_proxy_account##”凭据是否存在并包含有效信息。

当我检查时,我在 Database Engine -> Security -> Credentials 中没有凭据。

我想使用这样的命令:

EXEC sp_xp_cmdshell_proxy_account 'IIS APPPOOL\DefaultAppPool', 'password'

但我不知道我应该在“密码”中写什么。有什么建议吗?

这是我的 CubeController:

public ActionResult Index()
{            
    string sql = "execute master..xp_cmdshell 'dtexec /DTS \"\\MSDB\\B_Costs\" /SERVER . /CHECKPOINTING OFF /REPORTING V'";
    try
    {
        MyEntity.Database.ExecuteSqlCommand(sql);
    }
    catch (Exception e)
    {
        string error = "There was an error: <br />" + e.Message;
        TempData["Message"] = error;
    }

    return RedirectToAction("Index","Home");
}

【问题讨论】:

    标签: asp.net tsql xp-cmdshell


    【解决方案1】:

    希望下面的这个链接能够为您提供帮助。请让我知道它是否有效。

    http://www.databasejournal.com/features/mssql/xpcmdshell-for-non-system-admin-individuals.html

    After you set up a proxy account your non-sysadmin logins might still not be able to use xp_cmdshell.    If you have not granted your non-sysadmin user EXECUTE permissions on xp_cmdshell you will get this error:
    
    Msg 229, Level 14, State 5, Procedure xp_cmdshell, Line 1
    The EXECUTE permission was denied on the object 'xp_cmdshell', database 'mssqlsystemresource', schema 'sys'.
    To overcome this error all you have to do is make sure the non-sysadmin user has a database user in the master database and then GRANT your non-sysadmin user the rights to execute xp_cmdshell.  In order to do that all you have to do is run the following TSL code:
    
    USE master;
    GO
    CREATE USER [MyDomain\Dorothy] FOR LOGIN [MyDomain\Dorothy];
    GRANT EXECUTE ON xp_cmdshell TO [MyDomain\Dorothy];
    

    就我而言,我尝试过:

    GRANT EXECUTE ON xp_cmdshell TO [IIS APPPOOL\DefaultAppPool]
    

    我同意

    默认应用程序池

    请参阅屏幕截图以供参考: http://postimg.org/image/yqcf3vya1/

    结果是成功的。我使用我的 sa 或管理员帐户通过 SQL Server Management Studio 登录到我的数据库来执行GRANT EXECUTE。请尝试让我知道结果。谢谢

    【讨论】:

    • 我写了:CREATE USER [IIS APPPOOL\DefaultAppPool] FOR LOGIN [IIS APPPOOL\DefaultAppPool]; 我有错误:当前数据库中已经存在用户、组或角色“IIS APPPOOL\DefaultAppPool”。
    • 然后在 xp_cmdshell 上运行 GRANT EXECUTE T​​O [IIS APPPOOL\DefaultAppPool];结果如何?
    • 哦,抱歉,我在问题中使用了撤销而不是授予。错误如上,有问题。
    • 我知道了,看看我的问题。 “并且在使用了这样的命令后:GRANT EXECUTE ON xp_cmdshell TO [IIS APPPOOL\DefaultAppPool] 我又遇到了一个错误:xp_cmdshell 代理帐户信息无法检索或无效...” 你看到了吗?我已经这样做了,但是我遇到了与代理相关的错误。
    • 是的,您使用哪个帐户执行该命令? SA 帐户还是管理员帐户?
    猜你喜欢
    • 2023-03-30
    • 2013-02-21
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多