This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| web-integration:c-sharp-example [2019/08/01 10:25] tjotov created | web-integration:c-sharp-example [2019/08/05 11:27] (current) tjotov | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== C# Example ====== | ====== C# Example ====== | ||
| - | C# relies on Web services for ADUCID - or WSA. | + | C# relies on Web services for ADUCID - or WSA, see [[web-integration:basics|Server side integration basics]] | 
| - | Web service is a standalone “application” which must be deployed on same server as AIM. Its default address is like this: | + | |
| - | < | + | Import WSDL into your project in Visual Studio. | 
| - | https:// | + | |
| - | </ | + | |
| - | + | ||
| - | There you will see WSA Interface homepage where you can download WSDL file to generate client. | + | |
| - | + | ||
| - | < | + | |
| - | https:// | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | For detailed methods / classes / types description see [[http:// | + | |
| - | + | ||
| - | === Import WSDL into your project in Visual Studio. | + | |
| Create an instance of **AducidApiJsonServiceClient**. | Create an instance of **AducidApiJsonServiceClient**. | ||
| Line 24: | Line 10: | ||
| <sxh csharp> | <sxh csharp> | ||
| - | var aducid = new AducidApiJsonServiceClient(); | + | System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; | 
| - | string host = " | + | context.Response.ContentType | 
| - | string query = context.Request.QueryString.ToString(); | + | |
| - | var queryParsed = HttpUtility.ParseQueryString(query); | + | |
| - | var generateQr | + | |
| - | var arg = new OperationArgument(); | + | |
| - | //return URL for iOS | + | |
| - | arg.peigReturnName = host + "/ | + | |
| - | //QR code picture size | + | |
| - | arg.qrCodePictureHeight = 300; | + | |
| - | arg.qrCodePictureWidth = 300; | + | |
| - | //tab helper If - if more authentication are in web browser in more tabs this help ti identify them - has to be unique | + | |
| - | arg.id = " | + | |
| - | arg.generateQrCodePicture = generateQr == "true"; | + | |
| - | //JSON response from WSA | + |  | 
| - | var response | + | //get current URI | 
| - | //parse and store authId | + |  | 
| - | dynamic responseObj | + |  | 
| - | //store authId in your session | + | var queryParsed = HttpUtility.ParseQueryString(query); | 
| - | context.Session["123"] = (string) responseObj.data.authId; | + | //should we display QR? | 
| - | context.Response.ContentType = " | + | var generateQr = queryParsed[" | 
| - | //send response | + | |
| - | context.Response.Write(response); | + | |
| - | </sxh> | + | |
| - | OperationArgument is a simple object | + | //create argument | 
| - | <sxh csharp> | + | var arg = new requestOperationArguments(); | 
| - | public class OperationArgument | + | // | 
| - | { | + | arg.peigReturnName | 
| - | public String | + | arg.qrCodePictureHeight | 
| - |  | + | arg.qrCodePictureWidth | 
| - |  | + | //ID to support multiple authentications in the same browser | 
| - |  | + | arg.id = Guid.NewGuid().ToString(); | 
| - |  | + | arg.generateQrCodePicture = generateQr == " | 
| - | } | + | |
| + | try | ||
| + |  | ||
| + | // | ||
| + | var response = aducid.startAuthenticationSession(arg); | ||
| + | //store authId in session - security | ||
| + | context.Session[arg.id] = response.authId; | ||
| + | context.Response.ContentType = " | ||
| + | //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< | ||
| + | //get error as JSON | ||
| + | var response = aducid.toErrorJson(errorResult, | ||
| + | //send JSON to Web API | ||
| + | context.Response.Write(response); | ||
| + | } | ||
| </ | </ | ||
| + | |||
| === Create WSA proxy === | === Create WSA proxy === | ||
| Just simple " | Just simple " | ||
| <sxh csharp> | <sxh csharp> | ||
| - | var aducid = new AducidApiJsonServiceClient(); | + | //We have to " | 
| - | string host = "http://" | + |  | 
| - | var response = aducid.getStatus((string)context.Session[" | + |  | 
| - | context.Response.ContentType = "text/plain"; | + | try | 
| - | context.Response.Write(response); | + | { | 
| + | 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 | ||
| + |  | ||
| + | |||
| + | //send response to Web API | ||
| + |  | ||
| + | } | ||
| + | catch (System.ServiceModel.FaultException e) | ||
| + | { | ||
| + |  | ||
| + | var msgFault = e.CreateMessageFault(); | ||
| + | var errorResult = msgFault.GetDetail< | ||
| + | //get error as JSON | ||
| + | var response = aducid.toErrorJson(errorResult, | ||
| + | // | ||
| + |  | ||
| + | } | ||
| </ | </ | ||
| Line 76: | Line 89: | ||
| <sxh csharp> | <sxh csharp> | ||
| - | var aducid = new AducidApiJsonServiceClient(); | + |  | 
| - | //read authId from you session | + |  | 
| - | string authId = (string)context.Session[" | + | var queryParsed = HttpUtility.ParseQueryString(query); | 
| - | var aducidResult = aducid.getResult(authId, | + | var id = queryParsed[" | 
| - | //parse json | + |  | 
| - | dynamic aducidResultObj = JsonConvert.DeserializeObject(aducidResult); | + | string authId = (string)context.Session[id]; | 
| - | // | + | context.Response.ContentType = "application/ | 
| - | var udi = aducidResultObj.data.userDatabaseIndex; | + | try | 
| - | //key to further ADUCID operetions on user object | + | { | 
| - | string | + | //call AIM to get operation result | 
| - | //prepare response | + |  | 
| - | var response = aducid.endAuthenticationSession(authId, | + | // | 
| - | context.Response.ContentType = "text/plain"; | + | var udi = aducidResult.userDatabaseIndex; | 
| - | //OK do something with your session | + | //authKey | 
| - | //... | + |  | 
| - | //send response | + | //finalize authentication and get respoonse form Web API | 
| - | context.Response.Write(response); | + | var response = aducid.endAuthenticationSession(authId, | 
| + | // | ||
| + | FormsAuthentication.SetAuthCookie(" | ||
| + | //send Web API the result - by default it will redirect to URI we provided earlier; for iOS PEIG opens this URI directly | ||
| + |  | ||
| + | } | ||
| + | catch (System.ServiceModel.FaultException e) | ||
| + | { | ||
| + |  | ||
| + | var msgFault = e.CreateMessageFault(); | ||
| + | var errorResult = msgFault.GetDetail< | ||
| + | //get error as JSON | ||
| + | var response | ||
| + | //send JSON to Web API | ||
| + | context.Response.Write(response); | ||
| + | } | ||
| </ | </ | ||
| === Prepare a web page === | === Prepare a web page === | ||
| - | This page should contain [[integration-api:start|ADUCID Integration | + | This page should contain [[web-integration:client-side|Client | 
| Provide start, check and proxy Url. | Provide start, check and proxy Url. | ||
| - | < | ||
| - |  | ||
| - |  | ||
| - |  | ||
| - | </ | ||
| Override what should be done after authentication / operation: | Override what should be done after authentication / operation: | ||
| - | <sxh> | + | < | 
| - |  | + | <script type=" | 
| + | var aducid = new ADUCID(); | ||
| + | |||
| + | $(document).ready(function () { | ||
| + | aducid.setAuthentication(); | ||
| + | //operation start URL | ||
| + | aducid.setStartOperationUrl(" | ||
| + | //operation check URL | ||
| + | aducid.setResultOperationUrl(" | ||
| + | //proxy helper URL | ||
| + | aducid.setProxyUrl(" | ||
| + | |||
| + | //text under QR code | ||
| + | aducid.showQRHint = function (param) { | ||
| + | $('# | ||
| + | } | ||
| + | |||
| + | //running operation texr | ||
| + | aducid.showRunningHint = function (param) { | ||
| + | $('# | ||
| + | } | ||
| + | |||
| + | //called when auth finished (except iOS) | ||
| + |  | ||
| + | alert(" | ||
| if (result.status == " | if (result.status == " | ||
| - |  | + | location.href = result.redirect; | 
| - | //location.reload(); | + | |
| } | } | ||
| else { | else { | ||
| - |  | + |  | 
| } | } | ||
| } | } | ||
| + | |||
| + | //handle PEIG not installed error | ||
| + | aducid.showPeigError = function () { | ||
| + | showDialog(" | ||
| + | location.reload(); | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | //handle communication or other error | ||
| + | aducid.handleError = function (result) { | ||
| + | showDialog(" | ||
| + | location.href = result.redirect; | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | //GUI mapping | ||
| + | //button associated to start auth | ||
| + | aducid.setButton($("# | ||
| + | //locatino of QR code img | ||
| + | aducid.setQrCode($("# | ||
| + | //location of text under QR code | ||
| + | aducid.setQrHint($("# | ||
| + | |||
| + | //start; false as we wait for button click or qr scan | ||
| + | aducid.init(false); | ||
| + | }); | ||
| + | </ | ||
| </ | </ | ||
| Line 120: | Line 193: | ||
| <sxh html> | <sxh html> | ||
| - | < | + | <div class=" | 
| + |  | ||
| <div class=" | <div class=" | ||
| <div class=" | <div class=" | ||
| Line 146: | Line 220: | ||
| </ | </ | ||
| </ | </ | ||
| + | </ | ||
| </ | </ | ||