Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: Eye that follow cursor of the mouse Pin
VisualLive5-Nov-10 0:01
VisualLive5-Nov-10 0:01 
QuestionCustomize VS2010 Start Page Pin
Kevin Marois4-Nov-10 8:48
professionalKevin Marois4-Nov-10 8:48 
AnswerRe: Customize VS2010 Start Page Pin
Nish Nishant4-Nov-10 9:45
sitebuilderNish Nishant4-Nov-10 9:45 
GeneralRe: Customize VS2010 Start Page Pin
RaviRanjanKr16-Nov-10 2:40
professionalRaviRanjanKr16-Nov-10 2:40 
Questionfull text search Pin
annie_bel4-Nov-10 3:31
annie_bel4-Nov-10 3:31 
AnswerRe: full text search Pin
JohnLBevan4-Nov-10 3:53
professionalJohnLBevan4-Nov-10 3:53 
AnswerRe: full text search Pin
Adam R Harris4-Nov-10 4:53
Adam R Harris4-Nov-10 4:53 
QuestionImpersonation using C# Pin
JohnLBevan4-Nov-10 1:45
professionalJohnLBevan4-Nov-10 1:45 
Hi Guys,

I'm working on a windows service which runs under a given service account. It writes files to shared areas on other machines, but needs to change the owner of these files to a different account. I can do this using the SetAccessControl as below:

//call SetOwner(@"\\servername\share\filename.ext",@"domain\username");
public static void SetOwner(string uri, string account)
{
    System.Security.AccessControl.FileSecurity security = File.GetAccessControl(uri);
    IdentityReference owner = new NTAccount(account);
    security.SetOwner(owner); 
    File.SetAccessControl(uri, security); 
}


However, this only works if the service account which I'm using is an administrator or backup operator on the remote machine (unless it's setting the owner to itself). I'd like to avoid giving this service account rights to too many machines, since that puts too much power in the hands of one account. Instead, I'd like to provide a username and password for the account which will be the owner of the file, and have the service impersonate this account when assigning ownership (e.g. in a similar way to how you could have credentials in a connection string for a database connection which didn't use trusted authentication). I'm looking for something like the code below, but have so far been unable to find an understandable example to work from.

//call SetOwner(@"\\servername\share\filename.ext",@"domain\username");
public static void SetOwner(string uri, string account)
{
    StartImpersonating(account);
    System.Security.AccessControl.FileSecurity security = File.GetAccessControl(uri);
    IdentityReference owner = new NTAccount(account);
    security.SetOwner(owner); 
    File.SetAccessControl(uri, security); 
    StopImpersonating();
}

static void StartImpersonating(string account, string password)
{
    System.Security.Principal.WindowsIdentity.Impersonate(account, password);
}
static void StartImpersonating(string account)
{
    string password = GetPasswordFromSecureStore(account);
    StartImpersonating(account, password);
}
static void StartImpersonating(string domain, string username)
{
    StartImpersonating(string.Format("{0}\\{1}",domain,username));
}
static void StopImpersonating()
{
    StartImpersonating(System.Environment.UserDomainName,System.Environment.UserName);
}

NB: the above code is just for illustrative purposes to show the sort of thing I'm looking for; I've no idea whether what's there is even close to what's required to acheive this.

Any advice, pointers, or intPtrs appreciated.

Thanks in advance,

JB
AnswerRe: Impersonation using C# Pin
Adam R Harris4-Nov-10 4:49
Adam R Harris4-Nov-10 4:49 
AnswerRe: Impersonation using C# Pin
Manfred Rudolf Bihy4-Nov-10 5:06
professionalManfred Rudolf Bihy4-Nov-10 5:06 
GeneralRe: Impersonation using C# Pin
JohnLBevan9-Nov-10 5:43
professionalJohnLBevan9-Nov-10 5:43 
GeneralRe: Impersonation using C# Pin
Manfred Rudolf Bihy12-Nov-10 2:07
professionalManfred Rudolf Bihy12-Nov-10 2:07 
QuestionServer based on AsyncCallback not running as expected Pin
Tichaona J4-Nov-10 1:08
Tichaona J4-Nov-10 1:08 
AnswerRe: Server based on AsyncCallback not running as expected Pin
jschell4-Nov-10 8:32
jschell4-Nov-10 8:32 
QuestionGot a working ICMP listener; but how does it work? Pin
nebbukadnezzar4-Nov-10 0:28
nebbukadnezzar4-Nov-10 0:28 
QuestionRetrieve Value from TextBox Which is created in the code behind Pin
HatakeKaKaShi3-Nov-10 22:54
HatakeKaKaShi3-Nov-10 22:54 
AnswerRe: Retrieve Value from TextBox Which is created in the code behind Pin
John Gathogo4-Nov-10 1:00
John Gathogo4-Nov-10 1:00 
AnswerRe: Retrieve Value from TextBox Which is created in the code behind Pin
alrosan4-Nov-10 8:05
alrosan4-Nov-10 8:05 
Questionhow to find toolstrip button on a form by name Pin
Tridip Bhattacharjee3-Nov-10 21:27
professionalTridip Bhattacharjee3-Nov-10 21:27 
AnswerRe: how to find toolstrip button on a form by name [modified] Pin
Eddy Vluggen3-Nov-10 22:30
professionalEddy Vluggen3-Nov-10 22:30 
AnswerRe: how to find toolstrip button on a form by name Pin
_Erik_4-Nov-10 5:47
_Erik_4-Nov-10 5:47 
QuestionAdd Custom Utility Toolbar To VS2010 Pin
Kevin Marois3-Nov-10 12:52
professionalKevin Marois3-Nov-10 12:52 
AnswerRe: Add Custom Utility Toolbar To VS2010 Pin
Henry Minute3-Nov-10 12:56
Henry Minute3-Nov-10 12:56 
Questiondebugging mode behaves differently than final build Pin
kruegs353-Nov-10 6:02
kruegs353-Nov-10 6:02 
AnswerRe: debugging mode behaves differently than final build Pin
_Erik_3-Nov-10 6:14
_Erik_3-Nov-10 6:14 

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.