Controller for certificate enrollment (Windows Store apps)

Shows code that implements a System.Web.Mvc controller for serving certificates.

Banking app web service for certificate enrollment

The Windows Store app for banking requires a web service that provides a certificate from a trusted authority.

The following code example shows how to implement a System.Web.Mvc controller that serves a Personal Information Exchange (PFX) certificate.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CERTCLILib;

namespace BankServer.Controllers
{
    public class EnrollmentController : Controller
    {
        internal abstract class Constants
        {
            // Constants for property IDs
            public const int CR_PROP_CAXCHGCERT = 15;
            //const int ants for data types and indexed status
            public const int PROPTYPE_BINARY = 0x3;
            //Constants for binary flags
            public const int CV_OUT_BASE64 = 0x1;
            public const int CR_DISP_ERROR = 0x1;
            public const int CR_DISP_DENIED = 0x2;
            public const int CR_DISP_ISSUED = 0x3;
            public const int CR_DISP_UNDER_SUBMISSION = 0x5;
            public const int CR_IN_BASE64HEADER = 0;
            public const int CR_IN_BASE64 = 0x1;
            public const int CR_IN_BINARY = 0x2;
            public const int CR_IN_ENCODEANY = 0xff;
            public const int CR_IN_FORMATANY = 0x00;
            public const int CR_IN_ROBO = 0x00200000;
            public const int CR_IN_PKCS10 = 0x100;
            public const int CR_IN_PKCS7 = 0x300;
            public const int CR_IN_CMC = 0x400;
            public const int CR_IN_FORMATMASK = 0xff00;

            public const int CR_IN_CLIENTIDNONE = 0x00400000;
            public const int CR_IN_CONNECTONLY = 0x00800000;

            public const int CR_OUT_BASE64HEADER = 0x0;
            public const int CR_OUT_BASE64 = 0x1;
            public const int FR_PROP_FULLRESPONSE = 1;  // Binary
        }

        [HttpPost]
        public JsonResult Submit(string username, string password, string request)
        {
            const string CAConfig = "Devt-StandAloneCA.enrolldev.nttest.microsoft.com\\enrolldev-Devt-StandAlone-CA";

            ICertRequest CertRequest = new CCertRequest();

            CertRequest.Submit(Constants.CR_IN_ENCODEANY,
                               request,
                               "",
                               CAConfig);

            string certificate = 
	      CertRequest.GetCertificate(Constants.CR_OUT_BASE64);

            var result = new JsonResult();
            result.Data = new { certificate = certificate };
            return result;
        }

	[HttpPost]
	public JsonResult GetPFX(string username)
        {
            // IX509Enrollment EnrollObj = new CX509Enrollment();
            // EnrollObj.InitializeFromTemplateName(X509CertificateEnrollmentContext.ContextUser, "User");
            // EnrollObj.Enroll();
            // string pfx = EnrollObj.CreatePFX("1111", 0, EncodingType.XCN_CRYPT_STRING_BASE64HEADER);  
	  string password = "1111";
	  string friendlyName = "My WoodGrove Bank Certificate";
	  string pfx = "-----BEGIN CERTIFICATE-----" +
"MIIOvwIBAzCCDnsGCSqGSIb3DQEHAaCCDmwEgg5oMIIOZDCCAd0GCSqGSIb3DQEH"+
"AaCCAc4EggHKMIIBxjCCAcIGCyqGSIb3DQEMCgECoIHMMIHJMBwGCiqGSIb3DQEM"+
"AQMwDgQIJ+Nwezsdl8sCAgfQBIGoaMEnoOeeyN09o99ySQDkqkf9xYIJMjINMWMQ"+
"FN4GywQMF7vULdpPMyia91gu0yfrRZqTgZyNkmitqhZC1J3GkeNp1t+ZS14in2Ga"+
"G8BD95U6W73CmQuSOME4V58b97KhmMcss6HTxnM1EPNIrJcVao7n7XGzXVTJ1xC4"+
"N2U9WJuVLd90tsy72dNSLEIU/XWKe5ZAtU1k0GC/W9gTu0DatRtUPk+M4SRvMYHj"+
"MBMGCSqGSIb3DQEJFTEGBAQBAAAAMF0GCSsGAQQBgjcRATFQHk4ATQBpAGMAcgBv"+
"AHMAbwBmAHQAIABTAG8AZgB0AHcAYQByAGUAIABLAGUAeQAgAFMAdABvAHIAYQBn"+
"AGUAIABQAHIAbwB2AGkAZABlAHIwbQYJKoZIhvcNAQkUMWAeXgBsAGUALQB1AHMA"+
"ZQByAF8AdgAzAC0AYgA3AGQAYgAwADkAOQBmAC0AYwA3ADAAMQAtADQAZABmADIA"+
"LQA5AGQANwA1AC0AYwBkADcAMAA1ADUAYwAyAGYAMgBmADgwggx/BgkqhkiG9w0B"+
"BwagggxwMIIMbAIBADCCDGUGCSqGSIb3DQEHATAcBgoqh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"an3V65YbQCHsV47DmOA2YCBGSquU07+0FJ+nfoE2pS1j+oeMP5uTZUilfsuL20CQ"+
"vN+b43HjSOWGdALdXf4ZHTiOzVBBvY3R1mo8PJGxZF8cQPZyy2LgUj2ExtMdb2Z0"+
"w7dVza8oG24G/hkSsY0k7r5rsdcRHTA7MB8wBwYFKw4DAhoEFOYmJxU3rLf2U8cH"+
"QzO7kSUoTdj4BBQ/7k48RM8bkZtt5E4wi+sgeMv1AAICB9A="+
"-----END CERTIFICATE-----";

            var result = new JsonResult();
            result.Data = new { 
	      pfx = pfx, 
	      friendlyName = friendlyName, 
	      password = password
	    };
            return result;
        }
    }
}


Related topics

System.Security.Cryptography.X509Certificates
System.Web.Mvc
CertificateEnrollmentManager
PasswordVault
Working with certificates
Creating a Windows Store app for banking
Create a Windows Store app for banking: resources

 

 

Build date: 6/11/2013

Show:
© 2014 Microsoft