-
January 22nd, 2008, 11:50 AM
#1
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?
-
January 22nd, 2008, 11:58 AM
#2
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.
-
January 22nd, 2008, 12:09 PM
#3
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.
-
January 22nd, 2008, 12:22 PM
#4
Re: c# Windows Service working in Debug mode, but exception in SCM
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.
-
January 22nd, 2008, 12:41 PM
#5
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.
-
January 22nd, 2008, 12:45 PM
#6
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.
-
January 22nd, 2008, 02:39 PM
#7
Re: c# Windows Service working in Debug mode, but exception in SCM
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?
-
January 23rd, 2008, 05:37 AM
#8
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.
-
January 23rd, 2008, 12:09 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|