473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Access HKCU from an app with another set of credentials

I have looked at the documentation for 'Microsoft.Win32.Registry' and
'System.Security.Permissions.RegistryPermission' but am still stuck, hoping
some can give me a nudge in the right direction.

I would like to be able to modify the GPO Policy keys under HKCU for the
logged user from an application. Because they are read only to the user can
I prompt for an ID and PW (say a support tech) that can be used to access
the keys with read/write permissions?

Thanks Dan Rhoads
Nov 21 '05 #1
8 2034
Hi,

Take a look at the registrypermission class
http://msdn.microsoft.com/library/de...classtopic.asp

Ken
-----------------
"MSDN Account" <ni********@nospam.nospam> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
I have looked at the documentation for 'Microsoft.Win32.Registry' and
'System.Security.Permissions.RegistryPermission' but am still stuck, hoping
some can give me a nudge in the right direction.

I would like to be able to modify the GPO Policy keys under HKCU for the
logged user from an application. Because they are read only to the user can
I prompt for an ID and PW (say a support tech) that can be used to access
the keys with read/write permissions?

Thanks Dan Rhoads

Nov 21 '05 #2
Hi

In addition to Ken's suggestion, what is the exact registry key you want to
change?
What is the registry key's permission setting? You may check it by
following the steps below.
1. run regedit
2. Navigate to the registry key node
3. right click on the key and select permissions, check it to see if you
have permission.

The HKCU is only valid for the current logon user, if another user logon,
the HKCU will be loaded for that user.
So also through we can call the logonuser API to run the currect process in
another user's credential, but this will also cause the OS to load another
user's HKCU.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3
Hi Ken,

I have already been there, I will check again, maybe I missed what I was
lookoing for.

Dan R

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
Hi,

Take a look at the registrypermission class
http://msdn.microsoft.com/library/de...classtopic.asp
Ken
-----------------
"MSDN Account" <ni********@nospam.nospam> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
I have looked at the documentation for 'Microsoft.Win32.Registry' and
'System.Security.Permissions.RegistryPermission' but am still stuck, hoping some can give me a nudge in the right direction.

I would like to be able to modify the GPO Policy keys under HKCU for the
logged user from an application. Because they are read only to the user can I prompt for an ID and PW (say a support tech) that can be used to access
the keys with read/write permissions?

Thanks Dan Rhoads

Nov 21 '05 #4
Peter,

GPO keys are stored in to two locations in HCKU (ignoring HKLM for now).

HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Policies
HKEY_CURRENT_USER\Software\Policies

These keys, and all sub keys are at a minimum full control to
<computername>\administrators and read only to the end user (provided the
user does not have membership in <computername>\administrators. The reason
it is read only to the end users is to prevent them from removing policy
from themselves.

Here is an example of usage am I looking for:
The GPO applied to the end user has the "Disable registry editing tools"
policy enable which prevents a support tech from opening REGEDIT with the
user logged to review the user's HKCU hive. I would like to ask the support
tech for his credentials, which are in <computername>\administrators, to use
to access the keys above (in the users hive) to delete the policy that
disables the registry editing tools. This would temporarily allow access to
the end users HKCU hive. A GPUPDATE (or SECEDIT) could be run to restore
any key(s) there were removed. We have a fairly locked down user
environment and allowing the support tech to temporally remove policies
would be helpful for them.

Thanks,
Dan Rhoads
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:G2**************@TK2MSFTNGXA02.phx.gbl...
Hi

In addition to Ken's suggestion, what is the exact registry key you want to change?
What is the registry key's permission setting? You may check it by
following the steps below.
1. run regedit
2. Navigate to the registry key node
3. right click on the key and select permissions, check it to see if you
have permission.

The HKCU is only valid for the current logon user, if another user logon,
the HKCU will be loaded for that user.
So also through we can call the logonuser API to run the currect process in another user's credential, but this will also cause the OS to load another
user's HKCU.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #5
Hi Dan,

I am sorry that I ahve made a mistake about the LogonUser.
Based on my test, we can use the LogonUser and WindowsIdentity to
impersonate the current thread running at another high rights account .e.g
the administrator.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.MachineName
userName = "Test"
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userName, domainName,
"Password01!", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
tokenHandle)
Console.WriteLine("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("LogonUser failed with error code : {0}",
ret)
Console.WriteLine(ControlChars.Cr + "Error: [{0}] {1}" +
ControlChars.Cr, ret, GetErrorMessage(ret))
Return
End If

Dim success As String
If returnValue Then success = "Yes" Else success = "No"
Console.WriteLine(("Did LogonUser succeed? " + success))
Console.WriteLine(("Value of Windows NT token: " +
tokenHandle.ToString()))

' Check the identity.
Console.WriteLine(("Before impersonation: " +
WindowsIdentity.GetCurrent().Name))

Dim retVal As Boolean = DuplicateToken(tokenHandle,
SecurityImpersonation, dupeTokenHandle)
If False = retVal Then
CloseHandle(tokenHandle)
Console.WriteLine("Exception thrown in trying to duplicate
token.")
Return
End If

' TThe token that is passed to the following constructor must
' be a primary token in order to use it for impersonation.
Dim newId As New WindowsIdentity(dupeTokenHandle)
Dim impersonatedUser As WindowsImpersonationContext =
newId.Impersonate()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After impersonation: " +
WindowsIdentity.GetCurrent().Name))
Test()
' Stop impersonating the user.
impersonatedUser.Undo()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After Undo: " +
WindowsIdentity.GetCurrent().Name))
' Free the tokens.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
If Not System.IntPtr.op_Equality(dupeTokenHandle, IntPtr.Zero)
Then
CloseHandle(dupeTokenHandle)
End If
Catch ex As Exception
Console.WriteLine(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.CurrentUser.OpenSubKey("Software\Microsof t\Windows\CurrentVersion\P
olicies\Explorer\Test", True)
subkey.SetValue("Hello", 1)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToString())
End Try

Impersonate() 'Impersonate to another user to do the registry key
write..
End Sub

Also here is a detailed link about the issue, you may take at look.
How to impersonate a user in .NET (VB.NET, C#)
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemSecurityPrincipalWindowsIdentityClassIm personateTopic2.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #6
Peter,

No need to apologize, I could have been clearer in my first post. More
importantly THANK YOU! This was the nudge (you gave me more than I hoped
for!) I was looking for.

Dan Rhoads

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:Uq**************@TK2MSFTNGXA01.phx.gbl...
Hi Dan,

I am sorry that I ahve made a mistake about the LogonUser.
Based on my test, we can use the LogonUser and WindowsIdentity to
impersonate the current thread running at another high rights account .e.g
the administrator.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.MachineName
userName = "Test"
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userName, domainName,
"Password01!", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
tokenHandle)
Console.WriteLine("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("LogonUser failed with error code : {0}", ret)
Console.WriteLine(ControlChars.Cr + "Error: [{0}] {1}" +
ControlChars.Cr, ret, GetErrorMessage(ret))
Return
End If

Dim success As String
If returnValue Then success = "Yes" Else success = "No"
Console.WriteLine(("Did LogonUser succeed? " + success))
Console.WriteLine(("Value of Windows NT token: " +
tokenHandle.ToString()))

' Check the identity.
Console.WriteLine(("Before impersonation: " +
WindowsIdentity.GetCurrent().Name))

Dim retVal As Boolean = DuplicateToken(tokenHandle,
SecurityImpersonation, dupeTokenHandle)
If False = retVal Then
CloseHandle(tokenHandle)
Console.WriteLine("Exception thrown in trying to duplicate
token.")
Return
End If

' TThe token that is passed to the following constructor must
' be a primary token in order to use it for impersonation.
Dim newId As New WindowsIdentity(dupeTokenHandle)
Dim impersonatedUser As WindowsImpersonationContext =
newId.Impersonate()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After impersonation: " +
WindowsIdentity.GetCurrent().Name))
Test()
' Stop impersonating the user.
impersonatedUser.Undo()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After Undo: " +
WindowsIdentity.GetCurrent().Name))
' Free the tokens.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then CloseHandle(tokenHandle)
End If
If Not System.IntPtr.op_Equality(dupeTokenHandle, IntPtr.Zero)
Then
CloseHandle(dupeTokenHandle)
End If
Catch ex As Exception
Console.WriteLine(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.CurrentUser.OpenSubKey("Software\Microsof t\Windows\CurrentVersion\P olicies\Explorer\Test", True)
subkey.SetValue("Hello", 1)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToString())
End Try

Impersonate() 'Impersonate to another user to do the registry key
write..
End Sub

Also here is a detailed link about the issue, you may take at look.
How to impersonate a user in .NET (VB.NET, C#)
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemSecurityPrincipalWindowsIdentityClassIm personateTopic2.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #7
Peter,

Works perfect, thanks you!

Dan Rhoads
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:Uq**************@TK2MSFTNGXA01.phx.gbl...
Hi Dan,

I am sorry that I ahve made a mistake about the LogonUser.
Based on my test, we can use the LogonUser and WindowsIdentity to
impersonate the current thread running at another high rights account .e.g
the administrator.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.MachineName
userName = "Test"
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userName, domainName,
"Password01!", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
tokenHandle)
Console.WriteLine("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("LogonUser failed with error code : {0}", ret)
Console.WriteLine(ControlChars.Cr + "Error: [{0}] {1}" +
ControlChars.Cr, ret, GetErrorMessage(ret))
Return
End If

Dim success As String
If returnValue Then success = "Yes" Else success = "No"
Console.WriteLine(("Did LogonUser succeed? " + success))
Console.WriteLine(("Value of Windows NT token: " +
tokenHandle.ToString()))

' Check the identity.
Console.WriteLine(("Before impersonation: " +
WindowsIdentity.GetCurrent().Name))

Dim retVal As Boolean = DuplicateToken(tokenHandle,
SecurityImpersonation, dupeTokenHandle)
If False = retVal Then
CloseHandle(tokenHandle)
Console.WriteLine("Exception thrown in trying to duplicate
token.")
Return
End If

' TThe token that is passed to the following constructor must
' be a primary token in order to use it for impersonation.
Dim newId As New WindowsIdentity(dupeTokenHandle)
Dim impersonatedUser As WindowsImpersonationContext =
newId.Impersonate()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After impersonation: " +
WindowsIdentity.GetCurrent().Name))
Test()
' Stop impersonating the user.
impersonatedUser.Undo()

' Check the identity.
System.Diagnostics.Debug.WriteLine(("After Undo: " +
WindowsIdentity.GetCurrent().Name))
' Free the tokens.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then CloseHandle(tokenHandle)
End If
If Not System.IntPtr.op_Equality(dupeTokenHandle, IntPtr.Zero)
Then
CloseHandle(dupeTokenHandle)
End If
Catch ex As Exception
Console.WriteLine(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.CurrentUser.OpenSubKey("Software\Microsof t\Windows\CurrentVersion\P olicies\Explorer\Test", True)
subkey.SetValue("Hello", 1)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToString())
End Try

Impersonate() 'Impersonate to another user to do the registry key
write..
End Sub

Also here is a detailed link about the issue, you may take at look.
How to impersonate a user in .NET (VB.NET, C#)
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemSecurityPrincipalWindowsIdentityClassIm personateTopic2.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #8
Hi

I am glad that my suggestion helped you.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will...
4
by: Ian | last post by:
Can anyone help I have a web server on the internet with and ASP.NET application on it, the application is set to allow Anonymous Access and Integrated Windows. The Web.config is set to use...
5
by: Dave Kolb | last post by:
Is there any other solution for an ASPNET application to access network resources other than running as SYSTEM, using delegation (a nightmare to get to work) or the COM+ solution? I cannot seem to...
4
by: TrinityPete | last post by:
Hi all, We have a web application that uses web services for data access and retrieval. The web app and web services reside under IIS on the same server(WIN2003). The virtual directories have...
3
by: Robert May | last post by:
Here's the setup: Windows 2003 running .net 1.1 webservice with Integrated Authentication set. Client machine running .net 1.1 on xp pro with latest service packs and updates. User also has...
20
by: Newbie Coder | last post by:
MFC Application VC++.NET 2003 I have a certain registry key (HKCU\Software\MyKey) that contains between 30 & 64 string values I need to write a '*' to all those 30 - 64 string values under...
6
by: =?Utf-8?B?SWJyYWhpbS4=?= | last post by:
Hi, I have a client application which Accesses Web Service. but the Web service allows anonymous access to any client request (web/smart client). I want to authenticate every client request by...
3
by: =?Utf-8?B?Q2hyaXN0aWFuIGZyb20gRnJhbmNl?= | last post by:
I want to write an aspnet aspx page This page : 1 - Connect to a net share \\server\c$ with credentials: user/password 2 - Read Directory info of this share 3 - Write these infos in the...
2
by: tshad | last post by:
I am running a program as a Windows service which works fine. I am using a Mutex to prevent multiple threads from from accessing my log text file at the same time. It works fine in the Service:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.