Silverlight not sending Cookie to WCF Data Service RRS feed

  • Question

  • I have a Silverlight 5 (non business) project which uses Forms authentication. Authentication is being performed by the web page hosting the Silverlight control. In the web project, I have successfully created a Silverlight-enabled WCF Service which, after logging in, receives the correct Cookie header and sets the HttpContext.Current.User as expected. When I add a WCF Data Service (5.2.0) in the same web project Silverlight will not send the Cookie. I have looked at the article http://blogs.msdn.com/b/phaniraj/archive/2009/09/10/using-the-ado-net-data-services-silverlight-client-library-in-x-domain-and-out-of-browser-scenarios-ii-forms-authentication.aspx but in other more recent discussions, it sounds like the Cookie should be sent automatically. Any help would be appreciated.

    Tuesday, January 8, 2013 6:06 PM

All replies

  • Hi Johnny,

    Welcome to the MSDN forum.

    I am trying to involve another senior expert into your thread. Please wait the response . Sorry for any inconvenience.

    Have a nice day.

    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 7:55 AM
  • http://msdn.microsoft.com/en-us/data/gg192996

    http://msdn.microsoft.com/en-us/data/gg192996 white pages talks about Odata.

    We can use 
     HttpCookie AuthCookie = FormsAuthentication.GetAuthCookie(this.tbUsername.Text.Trim(), false);

    which can be used in the sample code
    HttpCookie cookie = Request.Cookies["sqlAuthCookie"];
            if (cookie == null)
                Response.Redirect("/appFolder/logon.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));


                Cookie authCookie = new Cookie(cookie.Name, cookie.Value);
                authCookie.Domain = "myServer";

               //NetworkCredential creds = this.Credentials as NetworkCredential;
               //string cookie = GetCookie(creds.UserName, creds.Password);
       string cookie = GetCookie(authCookie);  --> Not sure this is the right way to call it. You can take a help of ASP.NET team to get this code running 

     HttpCookie AuthCookie = FormsAuthentication.GetAuthCookie(this.tbUsername.Text.Trim(), false);

              e.RequestHeaders.Add("Cookie", cookie);

    Thursday, January 10, 2013 4:34 PM
  • Thanks, I appreciate it.
    Sunday, January 13, 2013 5:38 PM
  • I looked at this solution. It claims "Once the login is successful you can call the WCF Data Service in the usual way, without any special need to handle authentication tokens or pass in login credentials."  but in fact although authentication succeeds, only non-WCF data services are sent the cookie. For some reason, Silverlight won't send the Cookie when calling the WCF Data Service.
    Sunday, January 13, 2013 5:42 PM
  • This might be related to the setting of DataServiceContext.HttpStack (http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.httpstack(v=vs.95).aspx). See if changing it would fix the problem for you.


    Vitek Karas [MSFT]

    Sunday, January 13, 2013 8:46 PM
  • If I set the HttpStack from AutoHttp to HttpStack.xmlHttp I receive the following exception when calling the service (via LoadAsync)...

    System.Data.Services.Http.WebException occurred

      Message=Internal error at 'ScriptXmlHttpRequest.CreateNativeRequest'.
           at System.Data.Services.Http.ScriptXmlHttpRequest.CreateNativeRequest()
           at System.Data.Services.Http.ScriptXmlHttpRequest..ctor()
           at System.Data.Services.Http.XHRHttpWebRequest.InvokeRequest()
           at System.Data.Services.Http.XHRHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Data.Services.Client.HttpWebRequestMessage.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Data.Services.Client.HttpTransportLayer.BeginGetResponse(IODataRequestMessage requestMessage, AsyncCallback callback, Object state)
           at System.Data.Services.Client.ODataRequestMessageWrapper.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Data.Services.Client.BaseAsyncResult.InvokeAsync(Func`3 asyncAction, AsyncCallback callback, Object state)
           at System.Data.Services.Client.QueryResult.BeginExecuteQuery()
           at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state, String method)
           at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)
           at System.Data.Services.Client.DataServiceCollection`1.<>c__DisplayClass2.<LoadAsync>b__0(AsyncCallback asyncCallback)
           at System.Data.Services.Client.DataServiceCollection`1.BeginLoadAsyncOperation(Func`2 beginCall, Func`2 endCall)
           at System.Data.Services.Client.DataServiceCollection`1.LoadAsync(IQueryable`1 query)
           at PtcDashboard.ViewModels.OverviewViewModel.TestDataService()

    Monday, January 14, 2013 9:24 PM
  • That means you need the ClientStack in your particular case. So Auto does that for you as well. So sorry, that won't help.

    Vitek Karas [MSFT]

    Monday, January 14, 2013 9:55 PM