-->

Wednesday 17 August 2011

Fault Contracts in WCF services

Recently I was asked to raise a custom exception in the WCF Services which will be handled on the client side. As a normal programmer I created a custom exception in my service and left on the client side to handle. Assuming that it will be sent on the client side as it is. Instead it showed me this error.
Fault Contracts in WCF services

Little as I knew, something clicked me that if we pass/return the custom objects it has to be defined as a contract. As contracts are the only thing that WCF uses to communicate with the client.
I googled a bit around and came with the concept of Fault Contracts. Consider I have created a custom exception class as defined below
[DataContract()]
    public class CustomException
    {
        [DataMember()]
        public string Title;
        [DataMember()]
        public string ErrorMessage;
        [DataMember()]
        public int ErrorCode;
        [DataMember()]
        public string StackTrace;       
    }

Now if I throw this exception directly to the client, it would not recognize it. Hence any method which can raise this exception should specify the attribute while defining it. (as shown below)
[ServiceContract()]
    public interface IService
    {
        [OperationContract()]
        [FaultContract(typeof(CustomException))]
        String MyMethod(string inputParams);
    }
The next step is throwing the exception from the method
public String MyMethod (string inputParams)
        {
        CustomException error = new CustomException();
        error.Title = "my custom exception for fault contracts";
        error.ErrorMessage = "Error in MyMethod";
        error.ErrorCode = 1001;
        error.StackTrace = "custom stack trace";
        throw new FaultException(error,"Reason: Testing the Fault contract");
        }

While on the client side , you can catch the thrown exception as shown below,
        try
        {
        Proxy.MyMethod(“Kalashnikov”);
        }
        catch (FaultException<MyService.CustomException> ex)
        {
        //Process the Exception as thrown
        }

Custom Login page for Sharepoint 2010 Mixed mode authentication

In one of my previous post, I discussed about adding form authentication to your SharePoint site. I Hope it was useful ! This post is dedicated to customize the Login Screen for the SharePoint site


Recently I had to configure Windows authentication and forms authentication in SharePoint 2010. It should not have been difficult in 2010 as it have mixed mode authentication. But what lacked were enough tutorials/articles. So I thought to share it!
First of all we need a landing page for our application. It is good to have common landing page for windows authentication and SharePoint authentication. So that every unauthenticated request can be re-directed to single page. By default SharePoint have a dropdown to select the authentication mode and then proceeds based on selection.
Custom Login page for Sharepoint 2010 Mixed mode authentication
We can also have our own login page with the same functionality. I have designed something like this.
Custom Login page for Sharepoint 2010 Mixed mode authentication

You have to specify the login page URL in web.config. By default it is “/_login/default.aspx”
<forms loginUrl="/_layout/MyLoginPage.aspx" />
Note : MyLoginPage.aspx should be application page as user will not have access to SharePoint page,  as he /she is not authenticated yet.
As now as we have our login page ready, we need to add the code for login button and hyperlink to intranet access
1.       Forms Authentication via Login Button
2.       Windows Authentication via hyperlink ‘Click here for intranet access’

1.       Forms Authentication
In the event handler of login click event we can authenticate the user with the following method.
SPClaimsUtility.AuthenticateFormsUser(Context.Request.UrlReferrer,
                   Username,
                          Password);
This method authenticates a form user by checking username and password. When the user is validated this object persists a cookie to the client for later use.

2.       Windows Authentication
LnkIntranet.NavigateUrl = Page.ResolveUrl(@"~/_windows/default.aspx?ReturnUrl=/YouLandingPage.aspx");

SharePoint will authentication the windows logged in user
On Success it will redirect to the ‘ReturnUrl’ page mentioned in the parameter
On Error it will redirect to ‘AccessDenied.aspx’