Home All Groups Group Topic Archive Search About

Windows security context

Author
20 Apr 2005 4:29 PM
Oriane
Hi,

I would like to change the Windows security context of a thread, using this
code:

IntPtr pJeton;
if (LogonUser("myuser","europe","password2,0,out pJeton))  {

myWindowsIdentity = new WindowsIdentity (pJeton);

myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);

Thread.CurrentPrincipal = myWindowsPrincipal;

System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\myFile.txt");

MessageBox.Show(sr.ReadToEnd());

sr.Close();



I would expect that if the logon succeeds, and if "myuser" has not the NTFS
read permission on he file "myFile", a security  exception would be thrown.



But this is not the behaviour of this code, as if the underlying Windows
security contexte hasn't change and the read access is still check with my
Windows principal.

What have I miss ?

Orianre

Author
20 Apr 2005 6:14 PM
Dominick Baier [DevelopMentor]
Hello Oriane,

You have to call WindowsIdentity.Impersonate


---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> Hi,
>
> I would like to change the Windows security context of a thread, using
> this code:
>
> IntPtr pJeton;
> if (LogonUser("myuser","europe","password2,0,out pJeton))  {
> myWindowsIdentity = new WindowsIdentity (pJeton);
>
> myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
>
> Thread.CurrentPrincipal = myWindowsPrincipal;
>
> System.IO.StreamReader sr = new
> System.IO.StreamReader(@"C:\myFile.txt");
>
> MessageBox.Show(sr.ReadToEnd());
>
> sr.Close();
>
> I would expect that if the logon succeeds, and if "myuser" has not the
> NTFS read permission on he file "myFile", a security  exception would
> be thrown.
>
> But this is not the behaviour of this code, as if the underlying
> Windows security contexte hasn't change and the read access is still
> check with my Windows principal.
>
> What have I miss ?
>
> Orianre
>
Author
21 Apr 2005 8:23 AM
Oriane
Thanks

Show quoteHide quote
"Dominick Baier [DevelopMentor]" <dbaier@pleasepleasenospamdevelop.com>
wrote in message news:286126632496211369152352@news.microsoft.com...
> Hello Oriane,
>
> You have to call WindowsIdentity.Impersonate
>
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
> > Hi,
> >
> > I would like to change the Windows security context of a thread, using
> > this code:
> >
> > IntPtr pJeton;
> > if (LogonUser("myuser","europe","password2,0,out pJeton))  {
> > myWindowsIdentity = new WindowsIdentity (pJeton);
> >
> > myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
> >
> > Thread.CurrentPrincipal = myWindowsPrincipal;
> >
> > System.IO.StreamReader sr = new
> > System.IO.StreamReader(@"C:\myFile.txt");
> >
> > MessageBox.Show(sr.ReadToEnd());
> >
> > sr.Close();
> >
> > I would expect that if the logon succeeds, and if "myuser" has not the
> > NTFS read permission on he file "myFile", a security  exception would
> > be thrown.
> >
> > But this is not the behaviour of this code, as if the underlying
> > Windows security contexte hasn't change and the read access is still
> > check with my Windows principal.
> >
> > What have I miss ?
> >
> > Orianre
> >
>
>
>
Author
22 Apr 2005 12:43 AM
William Stacey [MVP]
But if just needing CAS, then you don't need Impersonate.  Is this correct?

--
William Stacey, MVP
http://mvp.support.microsoft.com

Show quoteHide quote
"Dominick Baier [DevelopMentor]" <dbaier@pleasepleasenospamdevelop.com>
wrote in message news:286126632496211369152352@news.microsoft.com...
> Hello Oriane,
>
> You have to call WindowsIdentity.Impersonate
>
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
>> Hi,
>>
>> I would like to change the Windows security context of a thread, using
>> this code:
>>
>> IntPtr pJeton;
>> if (LogonUser("myuser","europe","password2,0,out pJeton))  {
>> myWindowsIdentity = new WindowsIdentity (pJeton);
>>
>> myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
>>
>> Thread.CurrentPrincipal = myWindowsPrincipal;
>>
>> System.IO.StreamReader sr = new
>> System.IO.StreamReader(@"C:\myFile.txt");
>>
>> MessageBox.Show(sr.ReadToEnd());
>>
>> sr.Close();
>>
>> I would expect that if the logon succeeds, and if "myuser" has not the
>> NTFS read permission on he file "myFile", a security  exception would
>> be thrown.
>>
>> But this is not the behaviour of this code, as if the underlying
>> Windows security contexte hasn't change and the read access is still
>> check with my Windows principal.
>>
>> What have I miss ?
>>
>> Orianre
>>
>
>
>
Author
22 Apr 2005 9:19 AM
Dominick Baier [DevelopMentor]
Hello William Stacey [MVP],

what do you mean?

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> But if just needing CAS, then you don't need Impersonate.  Is this
> correct?
>
> "Dominick Baier [DevelopMentor]"
> <dbaier@pleasepleasenospamdevelop.com> wrote in message
> news:286126632496211369152352@news.microsoft.com...
>
>> Hello Oriane,
>>
>> You have to call WindowsIdentity.Impersonate
>>
>> ---------------------------------------
>> Dominick Baier - DevelopMentor
>> http://www.leastprivilege.com
>>> Hi,
>>>
>>> I would like to change the Windows security context of a thread,
>>> using this code:
>>>
>>> IntPtr pJeton;
>>> if (LogonUser("myuser","europe","password2,0,out pJeton))  {
>>> myWindowsIdentity = new WindowsIdentity (pJeton);
>>> myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
>>>
>>> Thread.CurrentPrincipal = myWindowsPrincipal;
>>>
>>> System.IO.StreamReader sr = new
>>> System.IO.StreamReader(@"C:\myFile.txt");
>>> MessageBox.Show(sr.ReadToEnd());
>>>
>>> sr.Close();
>>>
>>> I would expect that if the logon succeeds, and if "myuser" has not
>>> the NTFS read permission on he file "myFile", a security  exception
>>> would be thrown.
>>>
>>> But this is not the behaviour of this code, as if the underlying
>>> Windows security contexte hasn't change and the read access is still
>>> check with my Windows principal.
>>>
>>> What have I miss ?
>>>
>>> Orianre
>>>
Author
23 Apr 2005 1:47 AM
William Stacey [MVP]
I mean if your just creating the identity for code access security, you
don't need to do an os level impersonation (unless you need to impersonate
for OS level resources such as files, etc.)  Did I miss the point?  TIA

--
William Stacey, MVP
http://mvp.support.microsoft.com

Show quoteHide quote
"Dominick Baier [DevelopMentor]" <dbaier@pleasepleasenospamdevelop.com>
wrote in message news:291693632497618561627920@news.microsoft.com...
> Hello William Stacey [MVP],
>
> what do you mean?
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
>> But if just needing CAS, then you don't need Impersonate.  Is this
>> correct?
>>
>> "Dominick Baier [DevelopMentor]"
>> <dbaier@pleasepleasenospamdevelop.com> wrote in message
>> news:286126632496211369152352@news.microsoft.com...
>>
>>> Hello Oriane,
>>>
>>> You have to call WindowsIdentity.Impersonate
>>>
>>> ---------------------------------------
>>> Dominick Baier - DevelopMentor
>>> http://www.leastprivilege.com
>>>> Hi,
>>>>
>>>> I would like to change the Windows security context of a thread,
>>>> using this code:
>>>>
>>>> IntPtr pJeton;
>>>> if (LogonUser("myuser","europe","password2,0,out pJeton))  {
>>>> myWindowsIdentity = new WindowsIdentity (pJeton);
>>>> myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
>>>>
>>>> Thread.CurrentPrincipal = myWindowsPrincipal;
>>>>
>>>> System.IO.StreamReader sr = new
>>>> System.IO.StreamReader(@"C:\myFile.txt");
>>>> MessageBox.Show(sr.ReadToEnd());
>>>>
>>>> sr.Close();
>>>>
>>>> I would expect that if the logon succeeds, and if "myuser" has not
>>>> the NTFS read permission on he file "myFile", a security  exception
>>>> would be thrown.
>>>>
>>>> But this is not the behaviour of this code, as if the underlying
>>>> Windows security contexte hasn't change and the read access is still
>>>> check with my Windows principal.
>>>>
>>>> What have I miss ?
>>>>
>>>> Orianre
>>>>
>
>
>
Author
23 Apr 2005 4:10 PM
Dominick Baier [DevelopMentor]
Hello William Stacey [MVP],

CAS is based on the identity of the code - not the identity of the user running
the code...

maybe i am missing the point...

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> I mean if your just creating the identity for code access security,
> you don't need to do an os level impersonation (unless you need to
> impersonate for OS level resources such as files, etc.)  Did I miss
> the point?  TIA
>
> "Dominick Baier [DevelopMentor]"
> <dbaier@pleasepleasenospamdevelop.com> wrote in message
> news:291693632497618561627920@news.microsoft.com...
>
>> Hello William Stacey [MVP],
>>
>> what do you mean?
>>
>> ---------------------------------------
>> Dominick Baier - DevelopMentor
>> http://www.leastprivilege.com
>>> But if just needing CAS, then you don't need Impersonate.  Is this
>>> correct?
>>>
>>> "Dominick Baier [DevelopMentor]"
>>> <dbaier@pleasepleasenospamdevelop.com> wrote in message
>>> news:286126632496211369152352@news.microsoft.com...
>>>
>>>> Hello Oriane,
>>>>
>>>> You have to call WindowsIdentity.Impersonate
>>>>
>>>> ---------------------------------------
>>>> Dominick Baier - DevelopMentor
>>>> http://www.leastprivilege.com
>>>>> Hi,
>>>>>
>>>>> I would like to change the Windows security context of a thread,
>>>>> using this code:
>>>>>
>>>>> IntPtr pJeton;
>>>>> if (LogonUser("myuser","europe","password2,0,out pJeton))  {
>>>>> myWindowsIdentity = new WindowsIdentity (pJeton);
>>>>> myWindowsPrincipal = new WindowsPrincipal (myWindowsIdentity);
>>>>> Thread.CurrentPrincipal = myWindowsPrincipal;
>>>>>
>>>>> System.IO.StreamReader sr = new
>>>>> System.IO.StreamReader(@"C:\myFile.txt");
>>>>> MessageBox.Show(sr.ReadToEnd());
>>>>> sr.Close();
>>>>>
>>>>> I would expect that if the logon succeeds, and if "myuser" has not
>>>>> the NTFS read permission on he file "myFile", a security
>>>>> exception would be thrown.
>>>>>
>>>>> But this is not the behaviour of this code, as if the underlying
>>>>> Windows security contexte hasn't change and the read access is
>>>>> still check with my Windows principal.
>>>>>
>>>>> What have I miss ?
>>>>>
>>>>> Orianre
>>>>>