User Tools

Site Tools


web-integration:c-sharp-example

This is an old revision of the document!


C# Example

C# relies on Web services for ADUCID - or WSA, see Server side integration basics

Import WSDL into your project in Visual Studio.

Create an instance of AducidApiJsonServiceClient.

Create a start operation Url

This page starts authentication request.

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            context.Response.ContentType = "application/json";

            var aducid = new AducidApiServiceClient();
            //get current URI
            string host = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port;
            string query = context.Request.QueryString.ToString();
            var queryParsed = HttpUtility.ParseQueryString(query);
            //should we display QR?
            var generateQr = queryParsed["generateQrCodePicture"];

            //create argument
            var arg = new requestOperationArguments();
            //final URL for iOS 
            arg.peigReturnName = host + "/authenticated";
            arg.qrCodePictureHeight = 300;
            arg.qrCodePictureWidth = 300;
            //ID to support multiple authentications in the same browser
            arg.id = Guid.NewGuid().ToString();
            arg.generateQrCodePicture = generateQr == "true";

            try
            {
                //inititialize authentication and get response
                var response = aducid.startAuthenticationSession(arg);
                //store authId in session - security
                context.Session[arg.id] = response.authId;
                context.Response.ContentType = "application/json";
                //send repsponse to Web API
                context.Response.Write(aducid.toJson(response, ""));
            }
            catch (System.ServiceModel.FaultException e)
            {
                //handle error
                var msgFault = e.CreateMessageFault();
                var errorResult = msgFault.GetDetail<AducidClientException>().result;
                //get error as JSON
                var response = aducid.toErrorJson(errorResult, "error");
                //send JSON to Web API
                context.Response.Write(response);
            }

Create WSA proxy

Just simple “Rewrite” from WSA to C# with authId check

            //We have to "proxy" AIM reponses to Web APIuising this page
            var aducid = new AducidApiServiceClient();
            context.Response.ContentType = "application/json";
            try
            {
                string query = context.Request.QueryString.ToString();
                var queryParsed = HttpUtility.ParseQueryString(query);
                //get id from session based on GUID
                var id = queryParsed["id"];
                //get response from AIM
                var response = aducid.getStatus((string)context.Session[id]);
                
                //send response to Web API
                context.Response.Write(aducid.toJson(response, ""));
            }
            catch (System.ServiceModel.FaultException e)
            {
                //handle error
                var msgFault = e.CreateMessageFault();
                var errorResult = msgFault.GetDetail<AducidClientException>().result;
                //get error as JSON
                var response = aducid.toErrorJson(errorResult, "error");
                //send JSON to Web API
                context.Response.Write(response);
            }

Create check Url

On the result page we evaluate the authentication There are two basic results – authentication is OK or an error is thrown:

            var aducid = new AducidApiServiceClient();
            string query = context.Request.QueryString.ToString();
            var queryParsed = HttpUtility.ParseQueryString(query);
            var id = queryParsed["id"];
            //get authId from session
            string authId = (string)context.Session[id];
            context.Response.ContentType = "application/json";
            try
            {
                //call AIM to get operation result
                var aducidResult = aducid.getResult(authId, null);
                //User Database Index, i.e. UDI
                var udi = aducidResult.userDatabaseIndex;
                //authKey (with authId) used to access user data later from AIM
                var authKey2 = aducidResult.authKey;
                //finalize authentication and get respoonse form Web API
                var response = aducid.endAuthenticationSession(authId, authKey2);
                //AUTHENTICATED
                FormsAuthentication.SetAuthCookie("auth", false);
                //send Web API the result - by default it will redirect to URI we provided earlier; for iOS PEIG opens this URI directly
                context.Response.Write(aducid.toJson(response, "authenticated"));
            }
            catch (System.ServiceModel.FaultException e)
            {
                //handle error
                var msgFault = e.CreateMessageFault();
                var errorResult = msgFault.GetDetail<AducidClientException>().result;
                //get error as JSON
                var response = aducid.toErrorJson(errorResult, "error");
                //send JSON to Web API
                context.Response.Write(response);
            }

Prepare a web page

This page should contain ADUCID Integration API
Provide start, check and proxy Url. Override what should be done after authentication / operation:

 
     <script type="text/javascript">
        var aducid = new ADUCID();

        $(document).ready(function () {
            aducid.setAuthentication();
            //operation start URL
            aducid.setStartOperationUrl("open");
            //operation check URL
            aducid.setResultOperationUrl("openCheck");
            //proxy helper URL
            aducid.setProxyUrl("proxy");

            //text under QR code
            aducid.showQRHint = function (param) {
                $('#qr-code-hint').html("Use your PEIG to authenticate");
            }

            //running operation texr
            aducid.showRunningHint = function (param) {
                $('#qr-code-hint').html("Please wait ...");
            }

            //called when auth finished (except iOS)
            aducid.processResult = function (result) {
                alert("Auth finished. Result: " + JSON.stringify(result));
                if (result.status == "OK") {
                    location.href = result.redirect;
                }
                else {
                    location.href = result.redirect;
                }
            }

            //handle PEIG not installed error
            aducid.showPeigError = function () {
                showDialog("Problem with PEIG", "PEIG is not installed", "error", function () {
                    location.reload();
                });
            }

            //handle communication or other error
            aducid.handleError = function (result) {
                showDialog("General error", result.data.message, "error", function () {
                    location.href = result.redirect;
                });
            }

            //GUI mapping
            //button associated to start auth
            aducid.setButton($("#main-button"));
            //locatino of QR code img
            aducid.setQrCode($("#qr-code"));
            //location of text under QR code
            aducid.setQrHint($("#qr-code-hint"));

            //start; false as we wait for button click or qr scan
            aducid.init(false);
        });
    </script>

And create some HTML elements:

    <div class="container">
        <h1>ADUCID Hello World example</h1>
        <div class="row">
            <div class="col-xs-8 col-xs-offset-2">

                <div class="row">
                    <div class="col-xs-8 col-xs-offset-2">
                        <div id="main-button" class="btn btn-primary btn-lg center-block">Authenticate</div>
                    </div>
                </div>
                <div class="row">
                    <img id="qr-code" alt="" class="img-responsive center-block" />
                </div>

                <div class="row text-center">
                    <div id="qr-code-timer" class="qr-code-timer img-responsive center-block"></div>
                    <div id="qr-code-hint" class="qr-code-hint"></div>
                </div>

                <div id="peig-error" style="display: none;">
                    <h2>PEIG Error</h2>
                    <a class='btn  btn-lg' href='http://www.aducid.com/support/' data-lang="aducid_common_installPeig">Install PEIG</a>
                    <br />
                    <div class='btn btn-lg' onclick="location.reload()" data-lang="aducid_common_tryAgain">Try again</div>
                </div>
            </div>
        </div>
    </div>

web-integration/c-sharp-example.1565004357.txt.gz · Last modified: 2019/08/05 11:25 by tjotov