Click here to Skip to main content
15,896,154 members
Home / Discussions / C#
   

C#

 
AnswerRe: Get XmlElement of a property in a XmlSerializable-Class Pin
T M Gray3-May-10 11:59
T M Gray3-May-10 11:59 
GeneralRe: Get XmlElement of a property in a XmlSerializable-Class Pin
flitzpiepe23-May-10 20:47
flitzpiepe23-May-10 20:47 
QuestionLeft Top Ruler C# Pin
jojoba20113-May-10 5:27
jojoba20113-May-10 5:27 
AnswerRe: Left Top Ruler C# Pin
Pete O'Hanlon3-May-10 6:01
mvePete O'Hanlon3-May-10 6:01 
QuestionNeed to write a file to a server farm that is locked down by a ProcessID - this can't be THAT hard Pin
Alaric_3-May-10 5:02
professionalAlaric_3-May-10 5:02 
AnswerRe: Need to write a file to a server farm that is locked down by a ProcessID - this can't be THAT hard Pin
Michel Godfroid3-May-10 10:39
Michel Godfroid3-May-10 10:39 
GeneralRe: Need to write a file to a server farm that is locked down by a ProcessID - this can't be THAT hard Pin
Alaric_4-May-10 7:42
professionalAlaric_4-May-10 7:42 
AnswerRe: Need to write a file to a server farm that is locked down by a ProcessID - this can't be THAT hard Pin
Alaric_4-May-10 6:59
professionalAlaric_4-May-10 6:59 
meh...Impersonation it is then, I guess. This is a major shortcoming of the .NET platform. There is no excuse for this not to be encapsulated by the managed runtime. At any rate, if anyone needs to Impersonate a user, here is the code for a class that I threw together...DuplicateToken isn't even invoked, but I wrote it, so I'll include it.

using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;
using System.Web.Security;
using System.Web;
using System.Runtime.CompilerServices;
[assembly:AllowPartiallyTrustedCallers]

namespace NationallyKnownPoliticallyContentiousProjectThatIAmNotAtLibertyToDisclose.data
{
    public class ImpersonationManager
    {
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool DuplicateToken(IntPtr hToken, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr hDuplicateToken); 
 
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool RevertToSelf();

        public static string Domain { get; set; }
        public static string UserName { get; set; }
        public static string Password { get; set; }
        private static WindowsImpersonationContext _context;

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public static void BeginImpersonation()
        {
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;

            IntPtr tokenHandle = IntPtr.Zero;
            try
            {
                bool successfulLogin = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
                if (successfulLogin) //if !successful, there will be an exception
                {
                    WindowsIdentity identity = new WindowsIdentity(tokenHandle);
                    _context = identity.Impersonate();
                }
            }
            catch (Exception ex)
            {
                throw ex; //just trapping for quick debugging purposes. I put a breakpoint here.
            }
            finally
            {
                if (tokenHandle != IntPtr.Zero)
                    CloseHandle(tokenHandle);

            }
        }
        public static void EndImpersonation()
        {
            _context.Undo();
        }
    }
}


invocation looks something like this:
public static void DeliverOutbound(string document, MessageType messageInfo)
        {
            WindowsImpersonationContext context = null;
            try
            {
                string filePath = data.StaticDataClassTEMP.getFileSavePath(); //pulls from xml
                string archivePath = data.StaticDataClassTEMP.getArchiveSavePath(); //pulls from xml

                XmlDocument xDocument = new XmlDocument();
                xDocument.Load(HttpContext.Current.Server.MapPath("~/data/ApplicationSettings.xml"));

                ImpersonationManager.UserName = xDocument.DocumentElement.SelectSingleNode("username").InnerText;
                ImpersonationManager.Password = xDocument.DocumentElement.SelectSingleNode("password").InnerText;
                ImpersonationManager.Domain = xDocument.DocumentElement.SelectSingleNode("domain").InnerText;
                ImpersonationManager.BeginImpersonation();

                WriteArchive(document, messageInfo, archivePath);
                DeliverZip(document, messageInfo, filePath);
                //Do_Other_ProcessThatWouldGiveAwayNameOfProject();
                ImpersonationManager.EndImpersonation();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }


...and in ApplicationSettings (listing just to be completely thorough for those wanting an example)
<username>processID</username>
  <password>processPW</password>
  <domain>Corp</domain>

"I need build Skynet. Plz send code"

Questioni wait so much because of loops Pin
karayel_kara3-May-10 3:30
karayel_kara3-May-10 3:30 
AnswerRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 3:49
sitebuilderLuc Pattyn3-May-10 3:49 
AnswerRe: i wait so much because of loops Pin
Michel Godfroid3-May-10 4:04
Michel Godfroid3-May-10 4:04 
GeneralRe: i wait so much because of loops PinPopular
Luc Pattyn3-May-10 4:09
sitebuilderLuc Pattyn3-May-10 4:09 
AnswerRe: i wait so much because of loops [modified] Pin
William Winner3-May-10 8:15
William Winner3-May-10 8:15 
AnswerRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 8:24
mvePete O'Hanlon3-May-10 8:24 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 8:43
harold aptroot3-May-10 8:43 
GeneralRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 8:56
mvePete O'Hanlon3-May-10 8:56 
GeneralRe: i wait so much because of loops [modified] Pin
Luc Pattyn3-May-10 9:28
sitebuilderLuc Pattyn3-May-10 9:28 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:14
harold aptroot3-May-10 10:14 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:16
sitebuilderLuc Pattyn3-May-10 10:16 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:31
harold aptroot3-May-10 10:31 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:36
sitebuilderLuc Pattyn3-May-10 10:36 
GeneralRe: i wait so much because of loops Pin
harold aptroot3-May-10 10:42
harold aptroot3-May-10 10:42 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 10:56
sitebuilderLuc Pattyn3-May-10 10:56 
AnswerRe: i wait so much because of loops Pin
Pete O'Hanlon3-May-10 10:47
mvePete O'Hanlon3-May-10 10:47 
GeneralRe: i wait so much because of loops Pin
Luc Pattyn3-May-10 11:05
sitebuilderLuc Pattyn3-May-10 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.