窗户冒充[SQL,ASP.NET,C#] [英] Windows impersonate [SQL, ASP.NET, C#]

查看:134
本文介绍了窗户冒充[SQL,ASP.NET,C#]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Windows在ASMX Web服务冒充读取SQL数据库。


  1. 我的窗户使新帐户。

  2. 设置权限的数据库文件ORLDatabase.mdf完全控制。

现在我打电话的方法GetDataSet,但它在客户端与此错误完成:

System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.Exception的:在数据库中创建主拒绝DATABASE权限。
企图附加自动命名的数据库文件D:\\工作\\ web服务\\ App_Data文件\\ ORLDatabase.mdf失败。具有相同名称的数据库不存在,或者指定的文件无法打开,或它位于UNC共享。

我检查窗户,code冒充是 WindowsIdentity.GetCurrent(),目前的身份是good.The帐户对DATABSE文件的完整控制权,但它与错误finisch。
有人可以帮我,我dont't与SQL工作。我第一次尝试谷歌,但没有找到解决方案,它解决了我的问题。谢谢

 公共类服务:System.Web.Services.WebService
{
    公共TicketHeader票;
    公共数据集DS;    私人弦机=pcName;
    公共字符串username =********;
    公共字符串密码=*********;
    公共IntPtr的标记;
    公共WindowsImpersonationContext impersonationContext;    函数[DllImport(@D:\\ WINDOWS \\ SYSTEM32 \\ ADVAPI32.DLL)]
    公共静态的extern BOOL LogonUser的
    (字符串lpszUserName,串lpszDomain,串lpszPassword,诠释dwLogonType,诠释dwLogonProvider,OUT INT phToken);
    公共无效登录()
    {
        INT returnedToken;
        如果(LogonUser的(用户名,机,密码,3,0,出returnedToken))
        {
            令牌=新的IntPtr(returnedToken);
        }    }    [的WebMethod]
    公共数据集GetDataSet(字符串ID)
    {
        数据集DS = NULL;        登录();
        impersonationContext = WindowsIdentity.Impersonate(令牌);        康涅狄格州的SqlConnection = NULL;
        SqlDataAdapter的大= NULL;
        尝试
        {
            字符串SQL =选择表*;
            康恩=新的SqlConnection(@数据源= \\ SQLEX $ P $干燥综合征;集成安全=真;+
                    @AttachDbFilename = | DataDirectory目录| \\ ORLDatabase.mdf;);
            conn.Open();
            DA =新SqlDataAdapter的(SQL,conn);在
            DS =新的DataSet();
            da.Fill(DS,表);
        }
        赶上(异常前)
        {
            抛出新的异常(ex.Message);        }
        最后
        {
            如果(参数conn!= NULL)
                conn.Dispose();
        }        impersonationContext.Undo();
        返回DS;
    }
}


解决方案

Windows帐户创建需要在数据库引擎的登录为好。在SQL Server Management Studio中:服务器 - >安全 - >登录|右键 - >新建登录。我怀疑文件的权限就足够了。

I try use windows impersonate in asmx web service to read sql database.

  1. I make new account in windows.
  2. Set permission to full control on database file ORLDatabase.mdf.

Now I call method GetDataSet, but it finish on client side with this error:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: CREATE DATABASE permission denied in database 'master'. An attempt to attach an auto-named database for file D:\work\WebService\App_Data\ORLDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

I check windows impersonate in code with WindowsIdentity.GetCurrent(), the current identity is good.The Account have full control on databse file,but it finisch with error. Can somebody help me, I dont't work with sql. I try first google, but don't find solution which solve my problem. Thank

public class Service : System.Web.Services.WebService
{
    public TicketHeader Ticket;
    public DataSet ds;

    private string machine = "pcName";
    public string userName = "********";
    public string password = "*********";
    public IntPtr token; 
    public WindowsImpersonationContext impersonationContext;

    [DllImport(@"D:\Windows\System32\advapi32.dll")]
    public static extern bool LogonUser
    (string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);
    public void Login()
    {
        int returnedToken;
        if (LogonUser(userName, machine, password, 3, 0, out returnedToken))
        {
            token = new IntPtr(returnedToken);
        }

    }

    [WebMethod]
    public DataSet GetDataSet(string id)
    {
        DataSet ds = null;

        Login();
        impersonationContext = WindowsIdentity.Impersonate(token);

        SqlConnection conn = null;
        SqlDataAdapter da = null;
        try
        {
            string sql = "Select * from Table";
            conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=True;" +
                    @"AttachDbFilename=|DataDirectory|\ORLDatabase.mdf;");
            conn.Open();
            da = new SqlDataAdapter(sql, conn);
            ds = new DataSet();
            da.Fill(ds, "Table");
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            if (conn != null)
                conn.Dispose();
        }

        impersonationContext.Undo();
        return ds;
    }
}

解决方案

The windows account you created needs to be a login on the database engine as well. In SQL Server Management Studio: servername-->Security-->Login | Right Click --> New Login. I doubt file permissions are sufficient.

这篇关于窗户冒充[SQL,ASP.NET,C#]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆