Guest User

Untitled

a guest
Jul 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. how to work with Word Document in the C#
  2. public class CodeImpersonate
  3. {
  4. /// <summary>
  5. /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on by a terminal server,
  6. /// remote shell, or similar process. This logon type has the additional expense of caching logon information for disconnected operations; therefore,
  7. /// it is inappropriate for some client/server applications, such as a mail server.
  8. /// </summary>
  9. public const int LOGON32_LOGON_INTERACTIVE = 2;
  10. /// <summary>
  11. /// Use the standard logon provider for the system. The default security provider is negotiate,
  12. /// unless you pass NULL for the domain name and the user name is not in UPN format. In this case, the default provider is NTLM.
  13. /// Windows 2000: The default security provider is NTLM.
  14. /// </summary>
  15. public const int LOGON32_PROVIDER_DEFAULT = 0;
  16.  
  17. WindowsImpersonationContext impersonationContext;
  18.  
  19. [DllImport("advapi32.dll")]
  20. public static extern int LogonUserA(String lpszUserName,
  21. String lpszDomain,
  22. String lpszPassword,
  23. int dwLogonType,
  24. int dwLogonProvider,
  25. ref IntPtr phToken);
  26.  
  27. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  28. public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
  29.  
  30. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  31. public static extern bool RevertToSelf();
  32.  
  33. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  34. public static extern bool CloseHandle(IntPtr handle);
  35.  
  36. public bool ImpersonateValidUser(String userName, String domain, String password)
  37. {
  38. WindowsIdentity tempWindowsIdentity;
  39. IntPtr token = IntPtr.Zero;
  40. IntPtr tokenDuplicate = IntPtr.Zero;
  41.  
  42. if (RevertToSelf())
  43. {
  44. if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
  45. {
  46. if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
  47. {
  48. tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
  49. impersonationContext = tempWindowsIdentity.Impersonate();
  50. if (impersonationContext != null)
  51. {
  52. CloseHandle(token);
  53. CloseHandle(tokenDuplicate);
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. if (token != IntPtr.Zero)
  60. CloseHandle(token);
  61. if (tokenDuplicate != IntPtr.Zero)
  62. CloseHandle(tokenDuplicate);
  63. return false;
  64. }
  65.  
  66. public void UndoImpersonation()
  67. {
  68. if (impersonationContext != null)
  69. impersonationContext.Undo();
  70. }
  71.  
  72. CodeImpersonate codeImpersonate = null;
  73. try
  74. {
  75. codeImpersonate = new CodeImpersonate();
  76. var isLoggedIn = codeImpersonate.ImpersonateValidUser(AppConfigs.OfficeUser, AppConfigs.OfficeUeerDomnia, AppConfigs.OfficeUserPass);
  77.  
  78. if (isLoggedIn)
  79. {
  80. //Do your office work....
  81. }
  82. else
  83. throw new InvalidOperationException("Login failed for office user.");
  84. }
  85. finally
  86. {
  87. if (codeImpersonate != null)
  88. codeImpersonate.UndoImpersonation();
  89. codeImpersonate = null;
  90. }
  91.  
  92. public class ImpersonateServices
  93. {
  94. public ImpersonateServices(String userName, String domain, String password)
  95. {
  96. this.UserName = userName;
  97. this.Domain = domain;
  98. this.Password = password;
  99. }
  100.  
  101. public string UserName { get; private set; }
  102.  
  103. public string Domain { get; private set; }
  104.  
  105. public string Password { get; private set; }
  106.  
  107. public void Execute(Action privilegedAction)
  108. {
  109. CodeImpersonate codeImpersonate = null;
  110. try
  111. {
  112. codeImpersonate = new CodeImpersonate();
  113. var isLoggedIn = codeImpersonate.ImpersonateValidUser(this.UserName, this.Domain, this.Password);
  114.  
  115. if (isLoggedIn){
  116. privilegedAction();
  117. }
  118. else
  119. throw new InvalidOperationException("Login failed for office user.");
  120. }
  121. finally
  122. {
  123. if (codeImpersonate != null)
  124. codeImpersonate.UndoImpersonation();
  125. codeImpersonate = null;
  126. }
  127. }
  128. }
  129.  
  130. var impersonateServices = new ImpersonateServices(AppConfigs.OfficeUser,
  131. AppConfigs.OfficeUserDomain,
  132. AppConfigs.OfficeUserPass);
  133. impersonateServices.Execute(() => {
  134. //Do your Office work...
  135. });
Add Comment
Please, Sign In to add comment