PC Review


Reply
Thread Tools Rate Thread

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

 
 
John Lee
Guest
Posts: n/a
 
      11th Jan 2005
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


 
Reply With Quote
 
 
 
 
John Lee
Guest
Posts: n/a
 
      11th Jan 2005
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

"John Lee" <(E-Mail Removed)> wrote in message
news:OOWOARD%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      12th Jan 2005
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


 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      12th Jan 2005
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."

 
Reply With Quote
 
John Lee
Guest
Posts: n/a
 
      12th Jan 2005
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

"Kevin Yu [MSFT]" <v-(E-Mail Removed)> wrote in message
news:Y10cgIF%(E-Mail Removed)...
> 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."
>



 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      15th Jan 2005
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."

 
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
How to detect page called via back button =?Utf-8?B?SmVycnkgSg==?= Microsoft ASP .NET 1 25th Jan 2007 04:52 PM
USMT Detect component not detecting... =?Utf-8?B?Um9zcw==?= Windows XP Setup 1 12th Apr 2006 05:41 PM
How to detect when a Component is added to a WebForm David Microsoft ASP .NET 0 9th Mar 2005 04:36 PM
The system has called a custom component and that component has failed and generated an exception. lil_miss_sassy_girl@yahoo.com Microsoft Windows 2000 Developer 0 31st Jan 2005 05:23 PM
The system has called a custom component and that component has .. =?Utf-8?B?QW1pdCBCaGF0bmFnYXI=?= Microsoft Windows 2000 Advanced Server 2 17th Aug 2004 06:47 AM


Features
 

Advertising
 

Newsgroups
 


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