473,323 Members | 1,574 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,323 software developers and data experts.

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

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
Nov 16 '05 #1
5 3933
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" <jo***@newsgroup.nospam> wrote in message
news:OO****************@TK2MSFTNGP14.phx.gbl...
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

Nov 16 '05 #2
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
Nov 16 '05 #3
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."

Nov 16 '05 #4
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.GetCurre nt().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.GetCurre nt().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-****@online.microsoft.com> wrote in message
news:Y1***************@cpmsftngxa10.phx.gbl... 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."

Nov 16 '05 #5
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."

Nov 16 '05 #6

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

Similar topics

4
by: Michael Farber | last post by:
Not sure if this is the right group for this but anyways... I've got an ASP web application that uses a Visual Basic component to do some work. I instantiate the component in asp and then...
32
by: Victor | last post by:
I've been to a few websites where it displays the town, state (if USA), country I'm browsing from. Now, I know that detecting country is easy, but how do you determine the town? What ASP...
1
by: David Whitney | last post by:
Hi, all. I'm looking for a "straw poll" of opinions on two ways to implement part of an exception hierarchy. Forgive the silly example I offer, but its what came to mind on the fly as I decided how...
1
by: Amitava Sengupta | last post by:
Is there any issue in using COM component in web applications. The problem I'm facing are 1. Though the dll is using non-static member variables which are being instantiated on each call,...
2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
1
by: vishal™ | last post by:
I have come to know that bits is a concept being used for data transfer over the internet. I want to use this technology in my vb.net application. for this i tried to convert the 'BITS.IDL' file...
10
by: Ryan | last post by:
I'm creating a website that monitors the status of servers using My.Computer.Network.Ping. I'm looking for a way to fire off my Ping() function every second or so. I see a Timer control...
3
by: =?Utf-8?B?WHltb3g=?= | last post by:
I'm developing apps in WindowsXP which calls IBM's Websphere MQ Client. This client is a .NET application. My app is using an ActiveX wrapper in native Windows XP. When the application calls the...
0
by: bharathreddy | last post by:
In .Net COM+ components are referred to as serviced components, Namespace: System.EnterpriseServices; Advantage of Serviced Components: object pooling, database connection pooling,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.