0

My app enables external signers to sign documents. —Signers who are not employees. My app will create a CoSign digital certificate for them. Then they'll sign the document, then the user should be deleted from the system. I want my app to programmatically create/delete the users.

Is there a C# example of for the SAPI User Management API?

Jarrod Dixon
  • 15,727
  • 9
  • 60
  • 72

1 Answers1

0

Here's a C# example of how to get user info

namespace UMSample
{
    class Program
    {
        private const string ADMIN_USER = "AdminUser";
        private const string ADMIN_PASS = "AdminPass";

        static public int AddUser(string uid)
        {
            SAPIUM sapium = new SAPIUMClass();
            SAPICrypt sapicrypt = new SAPICrypt();
            int rc;

            if ((rc = sapicrypt.Init()) != 0) return 1;
            if ((rc = sapium.Init()) != 0) return 1;

            UMSESHandle hSes = null;
            // Handle Acquire
            if ((rc = sapium.HandleAcquire(out hSes)) != 0) return 1;

            // Logon as Administrator
            if ((rc = sapium.Logon(hSes, ADMIN_USER, "", ADMIN_PASS)) != 0)
            {
                sapium.HandleRelease(hSes);
                return 1;
            }

            string UserCN = uid + " Test User";
            string UserEmail = uid+"@demo.com";
            string Pwd = "12345678";

            if ((rc = sapium.UserAdd(
                hSes,
                uid,
                UserCN,
                UserEmail,
                Pwd,
                1)) != 0)
            {
                sapium.Logoff(hSes);
                sapium.HandleRelease(hSes);
                return 1;
            }

            /*----------------------------------*/
            // An example of how to get user info
            UserHandle uh;
            string s1, s2, s3, s4;
            int c1, c2, c3, mask = 0, updtime = 0;
            SAPI_UM_ENUM_USER_CERT_STATUS_TYPE certsts;
            SAPI_UM_ENUM_USER_LOGIN_STATUS loginstat;
            SAPI_UM_ENUM_USER_TYPE kind = SAPI_UM_ENUM_USER_TYPE.SAPI_UM_USER_TYPE_NONE;
            SAPI_UM_ENUM_USER_ENROLLMENT_STATUS enroll = SAPI_UM_ENUM_USER_ENROLLMENT_STATUS.SAPI_UM_NONE_USER_ENROLLMENT_STATUS;
            SAPI_UM_ENUM_USER_ENROLLMENT_REASON enrReason;
            SAPI_UM_ENUM_PENDING_REQUEST_STATUS_TYPE certrqsts;

            rc = sapium.UserGetByLoginName(hSes, uid, out uh);
            rc = sapium.UserInfoGetEx(hSes, uh, out s1, out s2, out s3, ref kind, ref mask, ref updtime, out s4,
                out enroll, out enrReason, out loginstat, out c1, out c2, out c3, out certsts, out certrqsts);
            /*----------------------------------*/

            sapium.Logoff(hSes);
            sapium.HandleRelease(hSes);

            return 0;
        }


        static public int DelUser(string uid)
        {
            SAPIUM sapium = new SAPIUMClass();
            int rc;

            if ((rc = sapium.Init()) != 0) return 1;

            UMSESHandle hSes = null;
            if ((rc = sapium.HandleAcquire(out hSes)) != 0) return 1;

            if ((rc = sapium.Logon(hSes, ADMIN_USER, "", ADMIN_PASS)) != 0)
            {
                sapium.HandleRelease(hSes);
                return 1;
            }

            if ((rc = sapium.UserDelete(hSes, uid)) != 0)
            {
                sapium.Logoff(hSes);
                sapium.HandleRelease(hSes);
                return 1;
            }

            sapium.Logoff(hSes);
            sapium.HandleRelease(hSes);

            return 0;
        }

        static void Main(string[] args)
        {
            int rc;

            for (int i = 0; i < 10; i++)
            {
                string uid = "Test" + i;
                rc = AddUser(uid);
                if (rc != 0) {
                    Console.WriteLine ("Error adding user: " + uid);
                    return;
                }

                rc = DelUser(uid);                
                if (rc != 0) {
                    Console.WriteLine ("Error deleting user: " + uid);
                    return;
                }
                Console.WriteLine("After handling user: " + uid);
            }

            Console.WriteLine("Press <Enter> to end");
            Console.ReadKey();
        }
    }
}
Larry K
  • 47,808
  • 15
  • 87
  • 140