CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2007
    Posts
    18

    c# Windows Service working in Debug mode, but exception in SCM

    I have written a C# Windows service and have added Microsoft.Web.Services3 as a reference.
    I need this to obtain a certificate from the certificate store. I.e.

    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.MaxAllowed);
    X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindByThumbprint, subject, false);
    try
    {
    cert = col[0];
    }
    catch (Exception e)
    {

    throw new Exception("Cert not found");
    }

    This code works fine when I run the service in debug mode, but when I add it to the Service Control Manager and start the service, it throws an exception on the Find(X509FindType.FindByThumbprint, subject, false) line.
    My question is why does it pass in debug mode while failing as a service?
    What is significant about the Find(X509FindType.FindByThumbprint, subject, false) that would throw an exception? Obviously it cannot find the cert but why?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# Windows Service working in Debug mode, but exception in SCM

    How are you debugging the service?

    Probably you are running under two different accounts: one under debug mode (your user account) and a different account when running under the SCM.

    To help when running under the SCM, you can use the services built-in eventlog functionality to write to the event log.

  3. #3
    Join Date
    Sep 2007
    Posts
    18

    Re: c# Windows Service working in Debug mode, but exception in SCM

    Hi Arjay,
    thanks for that, but how can you tell which account the SCM is running under?
    And maybe I need to change
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    to
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); for the SCM.
    Would that make sense?

    No, I have just tried, It does not work.
    Last edited by carrolls; January 22nd, 2008 at 12:12 PM.

  4. #4
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: c# Windows Service working in Debug mode, but exception in SCM

    Quote Originally Posted by carrolls
    And maybe I need to change
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    to
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); for the SCM.
    Would that make sense?
    Well, this change causes a different behavior. Keep in mind on a windows system you have different stores. Each user have one and each service as well because services will be executed in a user context. Additional there is a store for the whole machine where all users can accessing. But it is not possible that your service can have access to your private user store.
    So there are two solutions. You can put all certificates to the LocalMachine store which is not really good because anyone can access the certificates. Or your service has its own store and handles all certificate actions on your computer. Its a question of your requirements which solution you should use.

    For what is the service designed for?
    Useful or not? Rate my posting. Thanks.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# Windows Service working in Debug mode, but exception in SCM

    To find out which account the service is running, just look under the Logon tab inside the services control panel applet.

  6. #6
    Join Date
    Sep 2007
    Posts
    18

    Re: c# Windows Service working in Debug mode, but exception in SCM

    I have given the service the Username and password for the current user
    and now the service seems to work.

    But this will mean that other users who log onto the machine won't be able to use the service.
    Anyway, thanks for the help.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# Windows Service working in Debug mode, but exception in SCM

    Quote Originally Posted by carrolls
    But this will mean that other users who log onto the machine won't be able to use the service.
    Why not? How do the users interact with the service?

  8. #8
    Join Date
    Sep 2007
    Posts
    18

    Re: c# Windows Service working in Debug mode, but exception in SCM

    Simply because this code block within the service will not be able to find a certificate if the correct user for the service is not logged on.

    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.MaxAllowed);
    X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindByThumbprint, subject, false);
    try
    {
    cert = col[0];
    }
    catch (Exception e)
    {

    throw new Exception("Cert not found");
    }


    If I go into the SCM and change the Service account to the user currently logged in to the machine, it works.
    Last edited by carrolls; January 23rd, 2008 at 05:39 AM.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# Windows Service working in Debug mode, but exception in SCM

    Pass in the user credential data when the user interacts with the service.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured