Wiwiz Auth API Integration Sample (ASP.Net)

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="WiwizAuthApiSample_aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    User Name: <asp:TextBox ID="username" runat="server"></asp:TextBox>
    <br />
    Password: <asp:TextBox ID="pswd" runat="server" TextMode="Password"></asp:TextBox>
    <br />
    <asp:Button ID="login" runat="server" onclick="Login_Click" Text="Login" />
    <br />
    <asp:Label ID="LabelMsg" runat="server" Text=""></asp:Label>
    <br />
 
 
    </form>
</body>
</html>

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Net;
 
public partial class _Default : System.Web.UI.Page 
{
    string userkey = "246DD22C084BB40E";	// replace with your own user key
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        //****************************************************
        // Gets incoming parameters
        //****************************************************
 
        Session.Add("tokencode", Request.Params["tokencode"]);	    // incoming parameter "tokencode"
        Session.Add("srvurl", Request.Params["srvurl"]);	    	// incoming parameter "srvurl"
    }
 
    protected void Login_Click(object sender, EventArgs e)
    {
        //****************************************************
        // Step 1. Do your business. E.g. check user login ... 
        //****************************************************
 
        bool loginSuccess = false;
 
        //
        // Do something you need.
        // e.g. verify the user
        //      ......
        //
 
        loginSuccess = true; // assume login successful
 
	    if(loginSuccess == false) {
 
            LabelMsg.Text = "Login Failed!"; 	// if user login failed, show an error message  
 
	    } else {
 
            //****************************************************
            // Step 2. Do the pre-auth by calling Wiwiz Auth API
            // IMPORTANT: Do this on your server side(ASP, C#, JSP/Servlet, PHP...), 
            //            but DO NOT do this on your client side (HTML/Javascript)
            //****************************************************
 
            // parameter "action" : REQUIRED!
            // set it to "1" to authenticate the user
            // set it to "0" to block the user
		    string action = "1";
 
            // parameter "tokencode": REQUIRED!
            // set identical to the incoming parameter
            string tokencode = (string) Session["tokencode"];
 
            // parameter "srvurl": REQUIRED!
            // set identical to the incoming parameter
            string srvurl = (string) Session["srvurl"];
 
            // parameter "endtime" : OPTIONAL
            // Format: yyyy-mm-dd hh:MM:ss  e.g. 2012-05-31 21:39:00
            // set this parameter to set the time to close the user's Internet connection 
            // Note: the value must be url-encoded.  
		    string endtime = Server.UrlEncode("2014-05-31 21:39:00");
 
            // parameter "postauth" : OPTIONAL
            // E.g. http://www.YourDomain.com
            // set this parameter to redirect to a specified URL after authenticated.
            // Note: the value should be url-encoded. 
		    string postauth = "http://www.wiwiz.com";
 
		    string parameters =
                    "wiwiz_auth_api=1&ver=1.0" + // parameter "wiwiz_auth_api" and "ver". Fixed value
                    "&tokencode=" + tokencode +	// parameter "tokencode". See above
                    "&userkey=" + userkey +		// parameter "userkey". Set your own User Key
                    "&action=" + action +		// parameter "action". See above
                    "&endtime=" + endtime +		// parameter "endtime". See above
                    "&postauth=" + postauth; 	// parameter "postauth". See above
 
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(srvurl);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
 
            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(parameters);
            streamOut.Close();
 
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string verifycode = streamIn.ReadToEnd();            // gets "verifycode" - the verification result from Wiwiz server side
            streamIn.Close();
 
            string userstring = "username:" + username.Text;    // sets the Track Data (Optional), e.g. username
 
		    if(verifycode.StartsWith("ERR")) {
                // if there is an error, show error code
                LabelMsg.Text = "Error: " + verifycode;
 
		    } else {
                // OK, now. do Step 3.
 
			    //****************************************************
                // Step 3. Complete the Authentication by calling Wiwiz Auth API
			    //****************************************************	
                string redirectUrl = srvurl +		// use the value of incoming parameter "srvurl" as the redirection address
                        "?wiwiz_auth_api_login=1" +	// parameter "wiwiz_auth_api_login"
                        "&tokencode=" + tokencode +	// parameter "tokencode", set identical to the incoming parameter
                        "&verifycode=" + verifycode +	// parameter "verifycode", the verification result from Wiwiz server side
                        "&userstring=" + userstring;    // parameter "userstring" (Optional), the Track Data set by the user
 
                Response.Redirect(redirectUrl);	// finally, do the redirection
		    }
	    }
    }
}

Comments are closed.