Thursday, April 28, 2011

Trouble setting authentication Cookie for WCF Service

I am trying to set up a test project for a set of WCF services (which wrap a RIA Domain). I have an authentication service and another service which requires authentication that does all the work.

I've found a number of web sites that discuss how to get the authentication cookie from the authentication service and set it in the worker service but I am not having any luck using it. When I look at what is going on in Fiddler the response to the authentication service has what looks like a valid cookie. I set that cookie into the outgoing request for the data service using the code below. The first call to the data service has no cookie in the outbound header but the response has a cookie in it (which appears to be a session ID without the encrypted dtaa for authentication and roles etc.). Subsequent calls to the data service use the cookie session cookie that was returned in the first call in their header.

The code below was almost verbatim from Ben Morris and others. Essentially I get the cookie during Authenticate(...) and configure the outgoing header using GetProxy(). Any hints would be greatly appreciated.

     private AuthenticationServicesoapClient Authenticate(string userName, string password)     {       AuthenticationServicesoapClient authClient = new AuthenticationServicesoapClient();        using (new OperationContextScope(authClient.InnerChannel))       {         HttpRequestMessageProperty reqProp = new HttpRequestMessageProperty();         OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = reqProp;          QueryResultOfUser user = authClient.Login(userName, password, false, userName);         if (user != null && user.RootResults != null)         {           HttpResponseMessageProperty responseProperty = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];           string cookie = responseProperty.Headers[HttpResponseHeader.SetCookie];           authCookie = FormatCookie(cookie);                }         else return null;       }       return authClient;     }      private static string FormatCookie(string input)     {       string[] cookies = input.Split(new char[] { ',', ';' });       StringBuilder buffer = new StringBuilder(input.Length * 10);       foreach (string entry in cookies)       {         if (entry.IndexOf("=") > 0 && !entry.Trim().StartsWith("path") && !entry.Trim().StartsWith("expires"))         {           buffer.Append(entry).Append("; ");         }       }       if (buffer.Length > 0)       {         buffer.Remove(buffer.Length - 2, 2);       }       return buffer.ToString();     }      private PortalDomainsoapClient GetProxy()     {       PortalDomainsoapClient proxy = new PortalDomainsoapClient();       using (OperationContextScope scope = new OperationContextScope(proxy.InnerChannel))       {         HttpRequestMessageProperty request = new HttpRequestMessageProperty();         request.Headers.Add(HttpRequestHeader.Cookie, authCookie);         OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, request);       }             return proxy;     }  

Source: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4fdff207-9b88-496e-988c-0e23da85fb13

japanese architecture residential architecture architecture education architecture tools data center architecture

No comments:

Post a Comment