PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Detect in C# the name of the ASP.NET user.

 
 
=?Utf-8?B?VmlsaGVsbQ==?=
Guest
Posts: n/a
 
      9th Aug 2007
I am making a ASP.NET application that connects to a database.

I want to distribute this to my customers that may have different Windows
OS, so my installer must handle different windows versions.
The installer makes sure SQL Server 2005 is there, IIS is ready and ASP.NET
is set up properly.

In my installer I am using the following SQL statement to enable ASP.NET to
connect to my database.
CREATE LOGIN [NT AUTHORITY\NETWORK SERVICE] FROM WINDOWS
followed by
sp_grantdbaccess 'NT AUTHORITY\NETWORK SERVICE'

This works like a charm when the ASP.NET user is NT AUTHORITY\NETWORK
SERVICE, but that is not the case for all Windows versions.
It even depends on the language of the windows operation system.
Example: I tried it on a Swedish Windows 2003 Server, and then the user was
"NT INSTANS\NETWORK SERVICE"
Again, on other machines, the user is "machinename\ASPNET"

How do I detect (in C#) the name (including domain) of the user that runs
the ASP.NET process?

Thankful for any response
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Y3NoYXJwZXI=?=
Guest
Posts: n/a
 
      9th Aug 2007
This code may be helpful:

System.Security.Principal.WindowsIdentity.GetCurrent().Name;
 
Reply With Quote
 
=?Utf-8?B?VmlsaGVsbQ==?=
Guest
Posts: n/a
 
      9th Aug 2007
The problem is a little more complicated.

I don't need the name of the user running the current process.
I want to get the name of the user running the ASP.NET processes in IIS,
when I am running in an installer process probable running as an
Administrator.

How is that done?



"csharper" wrote:

> This code may be helpful:
>
> System.Security.Principal.WindowsIdentity.GetCurrent().Name;

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      9th Aug 2007
re:
!> I want to get the name of the user running the ASP.NET processes in IIS

Use WindowsIdentity.GetCurrent.Name() :

-------------
<%@ Page Language="VB" %>
<%@ Import NameSpace = System.Security.Principal %>
<script runat="server">
Sub Page_Load()
Dim tmp As String = WindowsIdentity.GetCurrent.Name()
Label1.Text = "ASP.NET is running as the account : " & tmp
End Sub
</script>
<html>
<head>
<title>What account is ASP.NET running as ?</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
-------------



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Vilhelm" <(E-Mail Removed)> wrote in message
news:3B433FF8-FC40-4123-915B-(E-Mail Removed)...
> The problem is a little more complicated.
>
> I don't need the name of the user running the current process.
> I want to get the name of the user running the ASP.NET processes in IIS,
> when I am running in an installer process probable running as an
> Administrator.
>
> How is that done?
>
>
>
> "csharper" wrote:
>
>> This code may be helpful:
>>
>> System.Security.Principal.WindowsIdentity.GetCurrent().Name;



 
Reply With Quote
 
=?Utf-8?B?VmlsaGVsbQ==?=
Guest
Posts: n/a
 
      10th Aug 2007

Using an ASP.NET process to get the name does not work for my case.
In my installer process (running as an administrator) I need to get the name
of the ASP.NET user.
The reason for this is that I need to grant ASP.NET SQL access, and that has
to happen in a process that already has SQL access, namely the installer
process.

This needs to happen automatically in my installer.
With code written in C#.

Any other suggestions?
Using SIDs somehow?
Quering machine.config somehow?
Asking IIS somehow?


"Juan T. Llibre" wrote:

> re:
> !> I want to get the name of the user running the ASP.NET processes in IIS
>
> Use WindowsIdentity.GetCurrent.Name() :
>
> -------------
> <%@ Page Language="VB" %>
> <%@ Import NameSpace = System.Security.Principal %>
> <script runat="server">
> Sub Page_Load()
> Dim tmp As String = WindowsIdentity.GetCurrent.Name()
> Label1.Text = "ASP.NET is running as the account : " & tmp
> End Sub
> </script>
> <html>
> <head>
> <title>What account is ASP.NET running as ?</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
> </div>
> </form>
> </body>
> </html>
> -------------
>
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en español : http://asp.net.do/foros/
> ======================================
> "Vilhelm" <(E-Mail Removed)> wrote in message
> news:3B433FF8-FC40-4123-915B-(E-Mail Removed)...
> > The problem is a little more complicated.
> >
> > I don't need the name of the user running the current process.
> > I want to get the name of the user running the ASP.NET processes in IIS,
> > when I am running in an installer process probable running as an
> > Administrator.
> >
> > How is that done?
> >
> >
> >
> > "csharper" wrote:
> >
> >> This code may be helpful:
> >>
> >> System.Security.Principal.WindowsIdentity.GetCurrent().Name;

>
>
>

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      10th Aug 2007
On Aug 10, 1:34 am, Vilhelm <Vilh...@discussions.microsoft.com> wrote:
> Using an ASP.NET process to get the name does not work for my case.
> In my installer process (running as an administrator) I need to get the name
> of the ASP.NET user.
> The reason for this is that I need to grant ASP.NET SQL access, and that has
> to happen in a process that already has SQL access, namely the installer
> process.
>
> This needs to happen automatically in my installer.
> With code written in C#.
>
> Any other suggestions?
> Using SIDs somehow?


If your installer is a Windows application, you can use
Process.GetProcesses() or Process.GetProcessesByName() methods to find
if aspnet_wp is running and get the account name associated with that
process (like in the Task Manager). Let me know if you need an example
of the code.


> Quering machine.config somehow?
> Asking IIS somehow?


As far as I remember on Win2003 identity is saved not in
machine.config

 
Reply With Quote
 
=?Utf-8?B?VmlsaGVsbQ==?=
Guest
Posts: n/a
 
      10th Aug 2007
Thanx a lot for the reply.

A code example would be great.
Is the aspnet_wp process always running when IIS and ASP.NET is set up, or
only when processing requests?

-- Vilhelm Heiberg

"Alexey Smirnov" wrote:

> On Aug 10, 1:34 am, Vilhelm <Vilh...@discussions.microsoft.com> wrote:
> > Using an ASP.NET process to get the name does not work for my case.
> > In my installer process (running as an administrator) I need to get the name
> > of the ASP.NET user.
> > The reason for this is that I need to grant ASP.NET SQL access, and that has
> > to happen in a process that already has SQL access, namely the installer
> > process.
> >
> > This needs to happen automatically in my installer.
> > With code written in C#.
> >
> > Any other suggestions?
> > Using SIDs somehow?

>
> If your installer is a Windows application, you can use
> Process.GetProcesses() or Process.GetProcessesByName() methods to find
> if aspnet_wp is running and get the account name associated with that
> process (like in the Task Manager). Let me know if you need an example
> of the code.
>
>
> > Quering machine.config somehow?
> > Asking IIS somehow?

>
> As far as I remember on Win2003 identity is saved not in
> machine.config
>
>

 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      10th Aug 2007
Hello Vilhelm,

> Thanx a lot for the reply.
>
> A code example would be great.
> Is the aspnet_wp process always running when IIS and ASP.NET is set
> up, or
> only when processing requests?


Only when there is an active appdomain. And under Windows 2003 and up, there
is no aspnet_wp process. It's all under w3wp.exe under windows 2003. Depending
on the settings of your IIS you can have multiple aspnet_wp or w3wp's running
under different credentials.

Wouldn't it be better to just show textbox and allow the installing user
to select the correct account used to run the application?

Jesse



>> On Aug 10, 1:34 am, Vilhelm <Vilh...@discussions.microsoft.com>
>> wrote:
>>
>>> Using an ASP.NET process to get the name does not work for my case.
>>> In my installer process (running as an administrator) I need to get
>>> the name
>>> of the ASP.NET user.
>>> The reason for this is that I need to grant ASP.NET SQL access, and
>>> that has
>>> to happen in a process that already has SQL access, namely the
>>> installer
>>> process.
>>> This needs to happen automatically in my installer. With code
>>> written in C#.
>>>
>>> Any other suggestions?
>>> Using SIDs somehow?

>> If your installer is a Windows application, you can use
>> Process.GetProcesses() or Process.GetProcessesByName() methods to
>> find if aspnet_wp is running and get the account name associated with
>> that process (like in the Task Manager). Let me know if you need an
>> example of the code.
>>
>>> Quering machine.config somehow?
>>> Asking IIS somehow?

>> As far as I remember on Win2003 identity is saved not in
>> machine.config
>>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      10th Aug 2007
On Aug 10, 10:46 am, Jesse Houwing <Jesse.houw...@nospam-sogeti.nl>
wrote:
> Hello Vilhelm,
>
> > Thanx a lot for the reply.

>
> > A code example would be great.
> > Is the aspnet_wp process always running when IIS and ASP.NET is set
> > up, or
> > only when processing requests?

>
> Only when there is an active appdomain. And under Windows 2003 and up, there
> is no aspnet_wp process. It's all under w3wp.exe under windows 2003. Depending
> on the settings of your IIS you can have multiple aspnet_wp or w3wp's running
> under different credentials.


Jesse is right. So, there can be a situation when aspnet_wp on IIS 5,
or w3wp on IIS 6 is not yet running (because for example the website
is not yet requested), or you can get more than one process because of
multiple applications

Here's the code to get User Name of the running process:

using System.Management;
using System.Security.Principal;

[Flags]
enum TOKEN_ACCESS : uint
{
TOKEN_QUERY = 0x0008
};

[DllImport("Advapi32.dll", SetLastError = true)]
extern static int OpenProcessToken(IntPtr processHandle, TOKEN_ACCESS
desiredAccess, out IntPtr tokenHandle);

[DllImport("kernel32.dll", SetLastError = true)]
extern static bool CloseHandle(IntPtr handle);

static void GetProc()
{
string[] processNames = { "aspnet_wp", "w3wp" };
bool ok = false;
foreach (string processName in processNames)
{
foreach (Process process in Process.GetProcessesByName(processName))
{
ok = true;
IntPtr token = IntPtr.Zero;
OpenProcessToken(process.Handle, TOKEN_ACCESS.TOKEN_QUERY, out token);
WindowsIdentity who = new WindowsIdentity(token);
CloseHandle(token);
Console.WriteLine("{0}", who.Name);
}
Console.WriteLine("Found: {0}", ok.ToString());
}

I check for "aspnet_wp" and "w3wp", but of course you can identify
what version of Windows is installed and use "w3wp" for Win2003 and
"aspnet_wp" for Win2000 and XP. If there are more than one process
were found then you will need to find somehow what application pool
will be used for your application.

>
> Wouldn't it be better to just show textbox and allow the installing user
> to select the correct account used to run the application?
>


I think it's a good idea

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
detect user Irene Microsoft Access Form Coding 11 27th May 2008 08:52 PM
detect the user Irene Microsoft Access 3 18th Apr 2008 07:38 PM
detect user Irene Microsoft Access Security 4 18th Apr 2008 02:05 AM
detect where the user goes?! =?Utf-8?B?RGFuaWVs?= Microsoft Access VBA Modules 3 7th Oct 2004 02:37 PM
Detect changes from user =?Utf-8?B?Qm9i?= Microsoft VB .NET 1 14th May 2004 02:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 AM.