locked
Reading USERNAME Windows Environment variable in COM+ and IIS RRS feed

  • Question

  • I have a COM+ application, which has set identity to a specific user and I have problem accessing environment variable %USERNAME%, because it gives me value SYSTEM instead of the username I specified under identity. When I look in task manager I see this:

    dllhost.exe SYSTEM 00 2.566 K COM Surrogate
    dllhost.exe*32 MyUser 00 37.729 K COM Surrogate

    I could say that there is almost the same situation in IIS, I have and webservice application, I set the identity on the application pool and here the environment variable %USERNAME% has value computer name with $ sign.


    I have searched and found many articles about environment variables, but none gave an answer to me.

    Has anyone experienced the same problem and how has he solved it?

    • Moved by Mike Feng Thursday, March 28, 2013 1:13 PM
    Wednesday, March 27, 2013 6:17 AM

Answers

All replies

  • You cannot use the envvar because the components are being hosted.  To access the identity of the user (app pool or otherwise) you should use WindowsPrincipal.  You can get that in various ways depending upon the host but Thread.CurrentPrincipal is one approach.  For a web app then you can use HttpContext.

    Michael Taylor - 3/27/2013
    http://msmvps.com/blogs/p3net

    Wednesday, March 27, 2013 2:30 PM
  • Hmm...it's not that I would like to use this variable, the problem is that on the machine is oracle client, which is configured to use wallets and the wallet location is defined with %USERNAME% (e.g. c:\oracle\wallets\%USERNAME%) variable, so that each windows user has it's own wallet. Permissons on %USERNAME% folder are set to each user. This configuration was working and it still (most of the time) works for COM+ application, for IIS webservice never did, it's allways computername$.

    So for COM+ application it's not true what you've said, it might for IIS application.

    Wednesday, March 27, 2013 3:17 PM
  • Your COM+ app is probably configured to run under the context of the user calling it (the default I believe).  Hence you'll have access to the env var.  It still behaves similar to IIS.

    Web apps run under the context of the app pool hosting them.  In most cases the app pool is configured for a local account or AppPoolIdentity which is going to have a user name you didn't expect.  Even if you switched the app pool to a fixed account the profile it runs under will be whatever that user account is.  If you're using Windows auth then the IIS site will properly set the identity context when the runtime begins processing the request.  However you'd have to have user profile loading enabled (the default) before the user's profile would be loaded and the var might be available.  I've never bothered to verify as I've never seen a case where it made sense to rely on the env var.

    I know nothing about Oracle wallets so I can not provide you a solution to your problem.  You should post this question on the Oracle forums where they can provide you a solution that works in your constraints.  The base approach of reading the user name envvar isn't going to work correctly.  You'll probably end up having to set some sort of override or perhaps change the identity that you're using to connect to Oracle.  The Oracle forums should be able to point you in the right direction.

    Michael Taylor - 3/27/2013
    http://msmvps.com/blogs/p3net

    Wednesday, March 27, 2013 3:45 PM
  • My COM+ app is configured like this:

    And the component activation tab is like this:

    Well, in your opinion the solution with env vars isn't the proper one? As I said, the COM+ app mostly get the correct value, but on a test machine, there is always uncorrect (SYSTEM), on some other there seems to be the problem sometimes, yet on another machine there always works ... All these machines are windows server 2008 and as I saw are configured in the same way - well there must be a difference I didn't notice.

    I'll ask on oracle forum how configure in such way that I want - well, it's not that I want, the customer has such requirements.

    Wednesday, March 27, 2013 4:28 PM
  • Hi Greo,

    Thank you for posting on MSDN Forum.

    Based on my understanding, this issue is out of ".NET Framework Class Libraries " forum scope, I moved it to "where is the forum for" to look for an appropriate forum.

    Thank you for your understanding and support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 1:13 PM
  • What about asking at http://forums.iis.com?

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Friday, March 29, 2013 4:30 AM
  • Well, what about Common Language Runtime Internals and Architecture  or Architecture General?

    I would like to find out how do environmet variables get loaded for service accounts - it can't not be the same as for interactive users, just beause of my problem I am experiencing.

    Where is the catch?

    Well COM+ and IIS are two different architectures, could we compare it - maybe from the point of that they are both services (IIS is  w3wp.exe serice and COM+ is dllhost.exe).

    Anyone more familiar with this?

    Friday, March 29, 2013 6:00 AM