none
Reading Certificate information in Windows Service RRS feed

  • Question

  • Hi,

    I have created windows service application, While starting the service i try to get the user certificate information using follwoing code. It is return nothing. but when i try to call the same set of code using windows application it returns the certificate. Can you provide any suggestion on why Windows service call not returning the certificate information. How we can get the certificate information in windows service application.

     

     

    Dim matchedCertificate As X509Certificate2 = Nothing

     

     

    Dim store As New X509Store(searchConfig.StoreName, searchConfig.StoreLocationEnumValue)

    store.Open(

     

    OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)

     

     

    Dim matchedCertificates As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)

     

     

    Dim findValue As Object

     

     

    ' Apply all search criterias

     

     

    For Each searchCriteria As SearchCriteria In searchConfig.SearchCriteria

     

     

    ' Resolve tokens in findValue

    findValue = ResolveTokens(searchCriteria.FindValue)

    matchedCertificates =

     

    DirectCast(matchedCertificates.Find(searchCriteria.FindTypeEnumValue, findValue, False), X509Certificate2Collection)

     

     

     

    Trace.WriteLine(String.Format("Found {0} certs with search criteria {1}={2}", matchedCertificates.Count, searchCriteria.FindType, findValue))

     

     

    If matchedCertificates.Count = 0 Then

     

     

    Throw New ArgumentException(String.Format("No matching certificate found in store location [{0}], store name [{1}], find type [{2}], find value [{3}]", searchConfig.StoreLocation, searchConfig.StoreName, searchCriteria.FindType, searchCriteria.FindValue))

     

     

    End If

     

     

    Next


    saravanakumar
    Wednesday, September 1, 2010 8:16 AM

Answers

  • Check which store you're trying to access. If you try to access anything different than LocalMachine, you won't be able to get the certificate from the service.

    Be sure to use that store for the certificates you want. And also check if you are using the correct StoreName


    Zaiden http://zPod.com.ar
    • Marked as answer by SamAgain Thursday, September 9, 2010 9:21 AM
    Thursday, September 2, 2010 5:50 PM

All replies

  • Check which store you're trying to access. If you try to access anything different than LocalMachine, you won't be able to get the certificate from the service.

    Be sure to use that store for the certificates you want. And also check if you are using the correct StoreName


    Zaiden http://zPod.com.ar
    • Marked as answer by SamAgain Thursday, September 9, 2010 9:21 AM
    Thursday, September 2, 2010 5:50 PM
  • I am trying to get the certificate from local machine only.... i am also search based on criteria... as i said it is working when same code call from windows application... if i tried using Windows serivice i was not able to get it. is there any settings change?
    saravanakumar
    Friday, September 3, 2010 5:48 AM
  • Just to know, is the service running with the SystemService account? And which store name are you using?
    Zaiden http://zPod.com.ar
    Friday, September 3, 2010 10:59 AM
  • Hi,

    We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Please mark the right answer at right time.
    Thanks,
    Sam
    Thursday, September 9, 2010 9:21 AM