How to detect if my component is being called from ASP.NET application?

J

John Lee

Hi,

I have a component with some static public methods and is it possible from
inside my method to decide if my component is invoked by an ASP.NET
application (such as web service) or a windows application? If YES, How to
do that?

If I know it's invoked by web service (web service is configured as windows
authentication only and assigned an application pool with NT domain account
as identity), Is the following assumption is 100% correct? or what is the
most reliable way of getting the authenticated user's name and the service
account's name?

1. Authenticated user will be
System.Threading.Thread.CurrentPrincipal.Identity.Name
2. Service account will be WindowsIdentity.GetCurrent().Name

If it's invoked by windows app, the authenticated user will be
WindowsIdentity.GetCurrent().Name, is this valid and reliable assumption?

Thanks a lot!
John
 
J

John Lee

I tried the following code -

if (System.Web.HttpContext.Current != null)
{
//invoked by ASP.NET app

}
else
{
//windows app
}

Is above solution 100% reliable?

Thanks!
John
 
R

Richard Blewett [DevelopMentor]

IIRC HttpContext.Currrent returns null if you are not hosted under ASP.NET but a valid context if you are.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I have a component with some static public methods and is it possible from
inside my method to decide if my component is invoked by an ASP.NET
application (such as web service) or a windows application? If YES, How to
do that?

If I know it's invoked by web service (web service is configured as windows
authentication only and assigned an application pool with NT domain account
as identity), Is the following assumption is 100% correct? or what is the
most reliable way of getting the authenticated user's name and the service
account's name?

1. Authenticated user will be
System.Threading.Thread.CurrentPrincipal.Identity.Name
2. Service account will be WindowsIdentity.GetCurrent().Name

If it's invoked by windows app, the authenticated user will be
WindowsIdentity.GetCurrent().Name, is this valid and reliable assumption?

Thanks a lot!
John
 
K

Kevin Yu [MSFT]

Hi John,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to check if the
component is called under ASP.NET or Windows service. If there is any
misunderstanding, please feel free to let me know.

Based on my experience, check for System.Web.HttpContext.Current is a
reliable method to check for the environment. IMO, it's better to use
System.Threading.Thread.CurrentPrincipal.Identity.Name to get the account
name. Since it gets the same thing as WindowsIdentity.GetCurrent().Name,
but it's more reliable under all circumstances.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

John Lee

Thanks Kevin,

That's exactly what I needed to confirm.

It's good to know that it's a reliable to check
System.Web.HttpContext.Current to detect the environment.

In ASP.NET environment, I found out the
System.Threading.Thread.CurrentPrincipal.Identity.Name returns the
Authenticated user and the
System.Security.Principal.WindowsIdentity.GetCurrent().Name returns the
identity of my web service account name.

In Windows Service environment, the
System.Threading.Thread.CurrentPrincipal.Identity.Name return System.Empty
and System.Security.Principal.WindowsIdentity.GetCurrent().Name returns the
service account Name.

Could you please confirm what account name can be retrieved more reliably in
all cases in your suggestion "IMO, it's better to use
System.Threading.Thread.CurrentPrincipal.Identity.Name to get the account
name. Since it gets the same thing as WindowsIdentity.GetCurrent().Name,
but it's more reliable under all circumstances."

Thanks very much!
John
reliable method to check for the environment
 
K

Kevin Yu [MSFT]

Hi John,

Please try to add the following line before tetting the CurrentPrinciple.

AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.Princip
alPolicy.WindowsPrincipal);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top