473,782 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access HKCU from an app with another set of credentials

I have looked at the documentation for 'Microsoft.Win3 2.Registry' and
'System.Securit y.Permissions.R egistryPermissi on' 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 2048
Hi,

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

Ken
-----------------
"MSDN Account" <ni********@nos pam.nospam> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
I have looked at the documentation for 'Microsoft.Win3 2.Registry' and
'System.Securit y.Permissions.R egistryPermissi on' 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***@bellsout h.net> wrote in message
news:eP******** ******@tk2msftn gp13.phx.gbl...
Hi,

Take a look at the registrypermiss ion class
http://msdn.microsoft.com/library/de...classtopic.asp
Ken
-----------------
"MSDN Account" <ni********@nos pam.nospam> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
I have looked at the documentation for 'Microsoft.Win3 2.Registry' and
'System.Securit y.Permissions.R egistryPermissi on' 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_US ER\Software\Mic rosoft\Windows\ CurrentVersion\ Policies
HKEY_CURRENT_US ER\Software\Pol icies

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.m icrosoft.com> wrote in message
news:G2******** ******@TK2MSFTN GXA02.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.
<PermissionSetA ttribute(Securi tyAction.Demand , Name:="FullTrus t")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.Mac hineName
userName = "Test"
Const LOGON32_PROVIDE R_DEFAULT As Integer = 0
Const LOGON32_LOGON_I NTERACTIVE As Integer = 2
Const SecurityImperso nation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userN ame, domainName,
"Password01 !", LOGON32_LOGON_I NTERACTIVE, LOGON32_PROVIDE R_DEFAULT,
tokenHandle)
Console.WriteLi ne("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLast Win32Error()
Console.WriteLi ne("LogonUser failed with error code : {0}",
ret)
Console.WriteLi ne(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.WriteLi ne(("Did LogonUser succeed? " + success))
Console.WriteLi ne(("Value of Windows NT token: " +
tokenHandle.ToS tring()))

' Check the identity.
Console.WriteLi ne(("Before impersonation: " +
WindowsIdentity .GetCurrent().N ame))

Dim retVal As Boolean = DuplicateToken( tokenHandle,
SecurityImperso nation, dupeTokenHandle )
If False = retVal Then
CloseHandle(tok enHandle)
Console.WriteLi ne("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 (dupeTokenHandl e)
Dim impersonatedUse r As WindowsImperson ationContext =
newId.Impersona te()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After impersonation: " +
WindowsIdentity .GetCurrent().N ame))
Test()
' Stop impersonating the user.
impersonatedUse r.Undo()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After Undo: " +
WindowsIdentity .GetCurrent().N ame))
' Free the tokens.
If Not System.IntPtr.o p_Equality(toke nHandle, IntPtr.Zero) Then
CloseHandle(tok enHandle)
End If
If Not System.IntPtr.o p_Equality(dupe TokenHandle, IntPtr.Zero)
Then
CloseHandle(dup eTokenHandle)
End If
Catch ex As Exception
Console.WriteLi ne(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.Curren tUser.OpenSubKe y("Software\Mic rosoft\Windows\ CurrentVersion\ P
olicies\Explore r\Test", True)
subkey.SetValue ("Hello", 1)
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToStr ing())
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/
frlrfSystemSecu rityPrincipalWi ndowsIdentityCl assImpersonateT opic2.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.m icrosoft.com> wrote in message
news:Uq******** ******@TK2MSFTN GXA01.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.
<PermissionSetA ttribute(Securi tyAction.Demand , Name:="FullTrus t")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.Mac hineName
userName = "Test"
Const LOGON32_PROVIDE R_DEFAULT As Integer = 0
Const LOGON32_LOGON_I NTERACTIVE As Integer = 2
Const SecurityImperso nation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userN ame, domainName,
"Password01 !", LOGON32_LOGON_I NTERACTIVE, LOGON32_PROVIDE R_DEFAULT,
tokenHandle)
Console.WriteLi ne("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLast Win32Error()
Console.WriteLi ne("LogonUser failed with error code : {0}", ret)
Console.WriteLi ne(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.WriteLi ne(("Did LogonUser succeed? " + success))
Console.WriteLi ne(("Value of Windows NT token: " +
tokenHandle.ToS tring()))

' Check the identity.
Console.WriteLi ne(("Before impersonation: " +
WindowsIdentity .GetCurrent().N ame))

Dim retVal As Boolean = DuplicateToken( tokenHandle,
SecurityImperso nation, dupeTokenHandle )
If False = retVal Then
CloseHandle(tok enHandle)
Console.WriteLi ne("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 (dupeTokenHandl e)
Dim impersonatedUse r As WindowsImperson ationContext =
newId.Impersona te()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After impersonation: " +
WindowsIdentity .GetCurrent().N ame))
Test()
' Stop impersonating the user.
impersonatedUse r.Undo()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After Undo: " +
WindowsIdentity .GetCurrent().N ame))
' Free the tokens.
If Not System.IntPtr.o p_Equality(toke nHandle, IntPtr.Zero) Then CloseHandle(tok enHandle)
End If
If Not System.IntPtr.o p_Equality(dupe TokenHandle, IntPtr.Zero)
Then
CloseHandle(dup eTokenHandle)
End If
Catch ex As Exception
Console.WriteLi ne(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.Curren tUser.OpenSubKe y("Software\Mic rosoft\Windows\ CurrentVersion\ P olicies\Explore r\Test", True)
subkey.SetValue ("Hello", 1)
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToStr ing())
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/ frlrfSystemSecu rityPrincipalWi ndowsIdentityCl assImpersonateT opic2.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.m icrosoft.com> wrote in message
news:Uq******** ******@TK2MSFTN GXA01.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.
<PermissionSetA ttribute(Securi tyAction.Demand , Name:="FullTrus t")> _
Private Sub Impersonate()
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName As String
domainName = Environment.Mac hineName
userName = "Test"
Const LOGON32_PROVIDE R_DEFAULT As Integer = 0
Const LOGON32_LOGON_I NTERACTIVE As Integer = 2
Const SecurityImperso nation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userN ame, domainName,
"Password01 !", LOGON32_LOGON_I NTERACTIVE, LOGON32_PROVIDE R_DEFAULT,
tokenHandle)
Console.WriteLi ne("LogonUser called.")
If False = returnValue Then
Dim ret As Integer = Marshal.GetLast Win32Error()
Console.WriteLi ne("LogonUser failed with error code : {0}", ret)
Console.WriteLi ne(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.WriteLi ne(("Did LogonUser succeed? " + success))
Console.WriteLi ne(("Value of Windows NT token: " +
tokenHandle.ToS tring()))

' Check the identity.
Console.WriteLi ne(("Before impersonation: " +
WindowsIdentity .GetCurrent().N ame))

Dim retVal As Boolean = DuplicateToken( tokenHandle,
SecurityImperso nation, dupeTokenHandle )
If False = retVal Then
CloseHandle(tok enHandle)
Console.WriteLi ne("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 (dupeTokenHandl e)
Dim impersonatedUse r As WindowsImperson ationContext =
newId.Impersona te()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After impersonation: " +
WindowsIdentity .GetCurrent().N ame))
Test()
' Stop impersonating the user.
impersonatedUse r.Undo()

' Check the identity.
System.Diagnost ics.Debug.Write Line(("After Undo: " +
WindowsIdentity .GetCurrent().N ame))
' Free the tokens.
If Not System.IntPtr.o p_Equality(toke nHandle, IntPtr.Zero) Then CloseHandle(tok enHandle)
End If
If Not System.IntPtr.o p_Equality(dupe TokenHandle, IntPtr.Zero)
Then
CloseHandle(dup eTokenHandle)
End If
Catch ex As Exception
Console.WriteLi ne(("Exception occurred. " + ex.Message))
End Try
End Sub

Private Sub Test()
Dim subkey As RegistryKey =
Registry.Curren tUser.OpenSubKe y("Software\Mic rosoft\Windows\ CurrentVersion\ P olicies\Explore r\Test", True)
subkey.SetValue ("Hello", 1)
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Try
Test() 'The line will fail, because the current user account
has not permission.
Catch ex As Exception
MsgBox(ex.ToStr ing())
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/ frlrfSystemSecu rityPrincipalWi ndowsIdentityCl assImpersonateT opic2.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
8207
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 obviously change once the program's fully built to actually do something, but for testing, it works). The only code I added to the service is below:
4
2700
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 Forms authentication. On the server side, where the web server is I have set IIS to disable Anonymous access and allow Integrated Windows. I have sync'd both the APSNET account on the web servers and application server so that access will be...
5
3112
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 impersonate a user and obtain network credentials using the DuplicateTokenEx call with appropriate parameters even though the call seems to not fail. I check my identity has changed but can only still do local commands. I would consider...
4
3613
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 been set for windows authentication and both the ASP web application and the web services are using windows authentication with impersonation of the logged on user. We also set the web service credentials to CredentialCache.DefaultCredentials. ...
3
2288
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 Solomon 6.0, Microsoft Office Basic and Microsoft Office Profesional, Acrobat reader, Adobe Acrobat. Otherwise, is a standard machine setup from Dell.
20
3801
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 that particular key. Example:
6
2403
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 passing username/password to make it more secure. Environment : Web Service/IIS5.0/ASP.NET 2.0 / C#. Kindly help with your suggestions.
3
8179
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 page
2
4870
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: In my AppSettings class: public static Mutex mutexPrinterFile; In my program:
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.