简体   繁体   中英

Windows Smart Card Authentication to Access Website-Data

I have to pull data from a website for a project. To access this page, a smartcard with a PIN is required. Unfortunately, at the moment I don't know how to implement this in C#.

Goal: 1. authentication at the website with the smartcard and PIN 2. GET/POST some Data

Could someone provide me with an approach to this issue.

UPDATE

When I try to access the website via the browser, I get the "normal" Windows SmartCard dialog displayed.

My goal is to write a console application that only executes a GET request on the page. The complete authentication should run in the background. The PIN of the smartcard can be hardcoded.

Okay, I've found the solution to my problem. If someone should have something similar in mind, I will provide my solution:

 X509Certificate2 cert = PickCertificate();

        var req = (HttpWebRequest)WebRequest.Create("//");

        req.CookieContainer = new CookieContainer(); 
        req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
        req.ClientCertificates.Add(cert);
        req.UseDefaultCredentials = true;

        ServicePointManager.ServerCertificateValidationCallback = (c, certificate, chain, errors) => true;

        var res = (HttpWebResponse)req.GetResponse();

        public static X509Certificate2 PickCertificate()
        {
        X509Certificate2 cert;
        var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

        store.Open(OpenFlags.ReadOnly);

        if (store.Certificates.Count == 1)
            cert = store.Certificates[0];
        else
            cert = X509Certificate2UI.SelectFromCollection(store.Certificates, "Caption", "Message", X509SelectionFlag.SingleSelection)[0];

        store.Close();
        return cert;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM