Guest User

Untitled

a guest
Apr 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public class Service : System.Web.Services.WebService
  2. {
  3. public TicketHeader Ticket;
  4. public DataSet ds;
  5.  
  6. private string machine = "pcName";
  7. public string userName = "********";
  8. public string password = "*********";
  9. public IntPtr token;
  10. public WindowsImpersonationContext impersonationContext;
  11.  
  12. [DllImport(@"D:WindowsSystem32advapi32.dll")]
  13. public static extern bool LogonUser
  14. (string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);
  15. public void Login()
  16. {
  17. int returnedToken;
  18. if (LogonUser(userName, machine, password, 3, 0, out returnedToken))
  19. {
  20. token = new IntPtr(returnedToken);
  21. }
  22.  
  23. }
  24.  
  25. [WebMethod]
  26. public DataSet GetDataSet(string id)
  27. {
  28. DataSet ds = null;
  29.  
  30. Login();
  31. impersonationContext = WindowsIdentity.Impersonate(token);
  32.  
  33. SqlConnection conn = null;
  34. SqlDataAdapter da = null;
  35. try
  36. {
  37. string sql = "Select * from Table";
  38. conn = new SqlConnection(@"Data Source=.SQLEXPRESS; Integrated Security=True;" +
  39. @"AttachDbFilename=|DataDirectory|ORLDatabase.mdf;");
  40. conn.Open();
  41. da = new SqlDataAdapter(sql, conn);
  42. ds = new DataSet();
  43. da.Fill(ds, "Table");
  44. }
  45. catch (Exception ex)
  46. {
  47. throw new Exception(ex.Message);
  48.  
  49. }
  50. finally
  51. {
  52. if (conn != null)
  53. conn.Dispose();
  54. }
  55.  
  56. impersonationContext.Undo();
  57. return ds;
  58. }
  59. }
Add Comment
Please, Sign In to add comment