Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Web Service Authentication

, 25 Nov 2009
A simple mechanism to authenticate users to a WebService
webserviceauthentication_src.zip
src
Global.asax
WebServiceAuthentication.csproj.webinfo
bin
WebServiceAuthentication.dll
Web References
ServicePointReference
Reference.map
ServicePoint.disco
ServicePoint.wsdl
src_VS2005
Global.asax
App_Code
App_WebReferences
ServicePointReference
WebServiceAuthentication
ServicePointReference
ServicePoint.disco
ServicePoint.discomap
ServicePoint.wsdl
src_VS2010
App_Code
App_WebReferences
ServicePointReference
WebServiceAuthentication
ServicePointReference
ServicePoint.disco
ServicePoint.discomap
ServicePoint.wsdl
Global.asax
WebServiceAuthentication.suo
using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;

namespace WebServiceAuthentication
{
    public partial class TestAuth : System.Web.UI.Page
    {

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //JUST FOR TEST: set the correct UserName & Password
                this.TextBoxUserName.Text = "MyUserName";
                this.TextBoxPwd.Text = "SeCrEt";
            }
        }

        protected void ButtonAuth_Click(object sender, System.EventArgs e)
        {
            string ret;
            string UserName, Password, Key, ToHash;

            UserName = this.TextBoxUserName.Text;
            Password = this.TextBoxPwd.Text;
            DateTime dt = DateTime.Now;
            ToHash = UserName.ToUpper() + "|" + Password + "|" + dt.ToString("yyyyMMdd") + "|" + dt.ToString("HHmm");
            Key = Hash(ToHash) + "|" + UserName + "|I would like to log this string in a DB";

            ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
            ret = Authenticate.Authenticate(Key);
            if (ret == null)
                this.ServResponse.Text = "NULL RECEIVED!!"; //never!
            else
            {
                this.ServResponse.Text = "RECEIVED DATA: " + ret;
            }
        }

        protected void ButtonGetToken_Click(object sender, System.EventArgs e)
        {
            string ret;

            ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
            ret = Authenticate.GetToken();
            this.TextBoxToken.Text = ret;
            string TokenDate;
            DateTime dt = DateTime.Now;
            TokenDate = dt.ToString("yyyy-MM-dd") + "|" + dt.ToString("HH:mm:ss");
            this.LabelTokendate.Text = " Token get at: " + TokenDate;
        }

        protected void ButtonService_Click(object sender, System.EventArgs e)
        {
            string ret;
            string UserName, Password, ServiceName;
            string Key, ToHash;

            UserName = this.TextBoxUserName.Text;
            Password = this.TextBoxPwd.Text;
            ServiceName = this.TextBoxService.Text;
            DateTime dt = DateTime.Now;
            ToHash = UserName.ToUpper() + "|" + Password + "|" + dt.ToString("yyyyMMdd") + "|" + dt.ToString("HHmm");
            Key = Hash(ToHash) + "|" + UserName + "|I would like to log this string in a DB";

            ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
            ret = Authenticate.UseService(Key, ServiceName);
            this.ServResponse.Text = ret;
        }

        protected void ButtonUseToken_Click(object sender, System.EventArgs e)
        {
            string ret;
            string UserName, Password, ServiceName, Token;
            string Key, ToHash;

            UserName = this.TextBoxUserName.Text;
            Password = this.TextBoxPwd.Text;
            ServiceName = this.TextBoxService.Text;
            Token = this.TextBoxToken.Text;
            ToHash = UserName.ToUpper() + "|" + Password + "|" + Token;
            Key = Hash(ToHash) + "|" + UserName + "|I would like to log this string in a DB";

            ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
            ret = Authenticate.UseService(Key, ServiceName);
            this.ServResponse.Text = ret;

            string NowDate;
            DateTime dt = DateTime.Now;
            NowDate = dt.ToString("yyyy-MM-dd") + "|" + dt.ToString("HH:mm:ss");
            this.LabelNowDate.Text = " >> Data time now: " + NowDate;
        }

        private string Hash(string ToHash)
        {
            // First we need to convert the string into bytes, which means using a text encoder.
            Encoder enc = System.Text.Encoding.ASCII.GetEncoder();

            // Create a buffer large enough to hold the string
            byte[] data = new byte[ToHash.Length];
            enc.GetBytes(ToHash.ToCharArray(), 0, ToHash.Length, data, 0, true);

            // This is one implementation of the abstract class MD5.
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);

            return BitConverter.ToString(result).Replace("-", "").ToLower();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

faina
Web Developer
Italy Italy
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Nov 2009
Article Copyright 2005 by faina
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid