Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

WCF Service Method Level Security using Message Contract

Rate me:
Please Sign up or sign in to vote.
4.74/5 (19 votes)
23 Jan 2012CPOL5 min read 115.7K   4K   49  
This article illustrates how to implement security for a service method, in the context of custom authentication, confidentiality and integrity, using Message Contract.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.225
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace TestMyService.MySvc {
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="MySvc.IMyService")]
    public interface IMyService {
        
        // CODEGEN: Generating message contract since message http://tempuri.org/IMyService/GetAccountsData requires protection.
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyService/GetAccountsData", ReplyAction="http://tempuri.org/IMyService/GetAccountsDataResponse")]
        TestMyService.MySvc.AccountsInfo GetAccountsData(TestMyService.MySvc.CustomerCredential request);
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    [System.ServiceModel.MessageContractAttribute(WrapperName="CustomerCredential", WrapperNamespace="http://tempuri.org/", IsWrapped=true, ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
    public partial class CustomerCredential {
        
        [System.ServiceModel.MessageHeaderAttribute(Namespace="http://tempuri.org/", ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign)]
        public string SecurityToken;
        
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", ProtectionLevel=System.Net.Security.ProtectionLevel.None, Order=0)]
        public string CustomerID;
        
        public CustomerCredential() {
        }
        
        public CustomerCredential(string SecurityToken, string CustomerID) {
            this.SecurityToken = SecurityToken;
            this.CustomerID = CustomerID;
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    [System.ServiceModel.MessageContractAttribute(WrapperName="AccountsInfo", WrapperNamespace="http://tempuri.org/", IsWrapped=true, ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign)]
    public partial class AccountsInfo {
        
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign, Order=0)]
        public string CustomerID;
        
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign, Order=1)]
        public string AccountNo;
        
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign, Order=2)]
        public double BalanceAmount;
        
        public AccountsInfo() {
        }
        
        public AccountsInfo(string CustomerID, string AccountNo, double BalanceAmount) {
            this.CustomerID = CustomerID;
            this.AccountNo = AccountNo;
            this.BalanceAmount = BalanceAmount;
        }
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface IMyServiceChannel : TestMyService.MySvc.IMyService, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class MyServiceClient : System.ServiceModel.ClientBase<TestMyService.MySvc.IMyService>, TestMyService.MySvc.IMyService {
        
        public MyServiceClient() {
        }
        
        public MyServiceClient(string endpointConfigurationName) : 
                base(endpointConfigurationName) {
        }
        
        public MyServiceClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public MyServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public MyServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        TestMyService.MySvc.AccountsInfo TestMyService.MySvc.IMyService.GetAccountsData(TestMyService.MySvc.CustomerCredential request) {
            return base.Channel.GetAccountsData(request);
        }
        
        public string GetAccountsData(string SecurityToken, ref string CustomerID, out double BalanceAmount) {
            TestMyService.MySvc.CustomerCredential inValue = new TestMyService.MySvc.CustomerCredential();
            inValue.SecurityToken = SecurityToken;
            inValue.CustomerID = CustomerID;
            TestMyService.MySvc.AccountsInfo retVal = ((TestMyService.MySvc.IMyService)(this)).GetAccountsData(inValue);
            CustomerID = retVal.CustomerID;
            BalanceAmount = retVal.BalanceAmount;
            return retVal.AccountNo;
        }
    }
}

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
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions