Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / C# 4.0

WCF Client Server Application with Custom Authentication, Authorization, Encryption and Compression - Part 1

Rate me:
Please Sign up or sign in to vote.
4.87/5 (46 votes)
22 Mar 2011CPOL8 min read 150.7K   5.9K   159  
HTTP - No IIS; Authentication - No SSL/X509 Certificate; Encryption - RSA+AES for Request, AES for Response; Compression - GZip for both Request/Response
/***********************/
/* (c) Alexandru Lungu */
/*  Challenge is Life! */
/* www.challenge-me.ws */
/***********************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;

namespace Challenge.Common
{
    public class Credentials
    {
        public const string CredentialsHeader = "Credentials";
        public const string CredentialsNamespace = "http://www.challenge-me.ws";
        public const int ExpiresTimeSpan = 10000; //miliseconds

        public string UserName { get; set; }
        public string Password { get; set; }
        public DateTime Expires { get; set; }

        #region Static

        public static TimeSpan ClientServerTimeSpan { get; private set; }

        public static DateTime ServerDateTime
        {
            get
            {
                return DateTime.Now - ClientServerTimeSpan;
            }
            set
            {
                ClientServerTimeSpan = DateTime.Now - value;
            }
        }

        public MessageHeader ToMessageHeader()
        {
            this.Expires = DateTime.Now.AddMilliseconds(ClientServerTimeSpan.TotalMilliseconds + ExpiresTimeSpan);
            MessageHeader header = MessageHeader.CreateHeader(CredentialsHeader, CredentialsNamespace, this);
            return header;
        }

        public static Credentials FromMessageHeader(Message message)
        {
            int tokenPosition = message.Headers.FindHeader(CredentialsHeader, CredentialsNamespace);
            Credentials credentials = message.Headers.GetHeader<Credentials>(tokenPosition);
            return credentials;
        }

        #endregion

    }
}

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)


Written By
Architect Adrem Automation
Romania Romania
Motto: “Challenge is Life!”

Software architect, developer, project manager, consultant - depending on the “Challenge”.

challenge-me.ws





Don't forget to vote or share your comments.

Comments and Discussions