SignOut with JavaScript
This is a minimal example of calling the SignOut API via HTML and Javascript. Remember to never use your Secret Keys on the client side.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<script type="text/javascript">
var connecterURL = "https://www.redcritterconnecter.com/";
function SignOut(sessionID, userID, cbSuccess, cbFail) {
$.getJSON(connecterURL + "services/management/signout?jsoncallback=?", { sessionID: sessionID, userID: userID},
function (data) {
if (data.Submitted) {
cbSuccess(data);
} else {
cbFail(data);
}
});
}
//My Success Callback
function onMySignOutSuccessHandler(data) {
//data is JSON response
alert("Request was successful");
}
//My Failure Callback
function onMySignOutFailHandler(data) {
//Something went wrong
alert("Something went wrong");
}
</script>
</head>
<body>
<table>
<tr>
<td>Session ID</td>
<td>
<input type="text" id="tSessionID" style="width: 500px" />
</td>
</tr>
<tr>
<td>UserID</td>
<td>
<input type="text" id="tUserID" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" onclick="SignOut($('#tSessionID').val(), $('#tUserID').val(), onMySignOutSuccessHandler, onMySignOutFailHandler)" value="Sign Out" /></td>
</tr>
</table>
</body>
</html>
SignOut with C#
This is a minmal example of calling the SignOut API and parsing the JSON result into a populated C# object. This example uses asynchronous techniques to raise a callback when the response is received.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web; //Add reference to System.Web
using System.Runtime.Serialization.Json; //Add reference to System.Runtime.Serialization
namespace RedCritterConnecter.Samples
{
//Create a class to contain the response
public class SignOutResponse
{
public String ErrorMessage { get; set; }
public Boolean Result { get; set; }
public String RequestID { get; set; }
public String APIVersion { get; set; }
public String ReturnParam1 { get; set; }
public String ReturnParam2 { get; set; }
public String ReturnParam3 { get; set; }
private Int32 ErrorCode { get; set; }
}
public class SignOut
{
public delegate void OnSignOutResponse(SignOutResponse SignOutResponse);
public event OnSignOutResponse onSignOutResponse;
public delegate void OnSignOutResponseError(SignOutResponse SignOutResponse);
public event OnSignOutResponseError onSignOutResponseError;
const string CONST_ConnecterBaseURL = "https://www.redcritterconnecter.com/";
public void Execute(String SessionID, String UserID)
{
try
{
//Create url encoded parameters in query string
String queryString = "sessionid=" + System.Web.HttpUtility.UrlEncode(SessionID) + "&userid=" + System.Web.HttpUtility.UrlEncode(UserID);
//Create a new instance of a WebClient
WebClient wc = new System.Net.WebClient();
//Prevent this request from caching in order to ensure that it is sent to server
wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
//Attach an event handler to receive the response
wc.DownloadStringCompleted += onSignOutResponseReceived;
//Make the call
wc.DownloadStringAsync(new Uri(CONST_ConnecterBaseURL + "services/management/SignOut?" + queryString, UriKind.Absolute));
}
catch
{
//Something went wrong communicating with the server
if (onSignOutResponseError != null)
{
onSignOutResponseError(null);
}
}
}
private void onSignOutResponseReceived(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//Create a JSON serializer
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(SignOutResponse));
//Copy the string into a memory stream
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
//Read the stream into an object matching the response's type
SignOutResponse SignOutResponse = (SignOutResponse)s.ReadObject(ms);
//Work with the populated response object
if (SignOutResponse.Result == true)
{
//Request was accepted, raise the success event
onSignOutResponse(SignOutResponse);
}
else
{
//Something went wrong
if (onSignOutResponseError != null)
{
//Request failed, raise the error event
onSignOutResponseError(SignOutResponse);
}
}
}
catch
{
//Something went wrong
onSignOutResponseError(null);
}
}
}
}