locked
Forms Authentication for DataServiceContext in client application RRS feed

  • Question

  • Hello,

    I developped an ASP.NET website with Forms authentication. If a visitor is not authenticated, he is automatically redirected to the login page.

    Within my web site project, I have also added an ADO.NET Data Service which is reachable when a user is authenticated.

    When I test the application within my browser, I have no problem. If I try to access to my ADO.NET Data Service without being authenticated, I am redirected to the login page. When I am authenticated, I am allow to use the ADO.NET Data Service.

    I have also developped a Windows Application which use the ADO.NET Data Services from my ASP.NET website. When I try to execute a query to retrieve data from my ADO.NET Data Service, it is failed because my Windows Application is not authenticated for the ASP.NET website. So, I am redirected to the login page and I received an exception.

    Error processing response's mime-type type 'text/html'.  The only supported mime-type is 'application/atom+xml'.

    Is there a way to configure the DataServiceContext in my Windows Application to make a Forms authentication on my Website when I execute query to retrieve data?

    Code Snippet

    ServiceReference.NorthwindEntities context = new Client.ServiceReference.NorthwindEntities(new Uri("http://localhost:9999/service.svc"));

    // how to configure the context?
    var query = from cat in context.Categories
    orderby cat ascending
    select cat;
    dataGridView1.DataSource = query.ToList(); // Exception received


    I have already searched a solution with Client Application Services but impossible to configure this correctly to automaticaly authenticate the HTTP requests sent by the DataServicecontext..

    I thank you in advance.

    Jérôme Lambert
    Friday, October 10, 2008 2:04 PM

Answers

  • Hi ,
     In Silverlight , using the Client Application services to authenticate your client browser will cause the Authentication cookie to be stored in the browser's cooke container.
     If you authenticate before you make the call to the DataService , then the authentication cookie will automatically get sent back to the server and this should work.
     Are you facing issues with authenticating with the web site from Silverlight ? or are you facing issues with sending the authentication cookie to the server ?
    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Thursday, August 13, 2009 5:01 PM
    Moderator

All replies

  • Hello,

     

    After one day of headache with a simple scenario of integrated windows authentication that I can't solve I ask the same question as Jérôme.

     

    Code Snippet

    ServiceReference.NorthwindEntities context = new Client.ServiceReference.NorthwindEntities(new Uri("http://localhost:9999/service.svc"));

    // how to configure the context?

     

     

    if someone can help or give a links it will be of great help.

     

    Thank you

    Friday, October 10, 2008 3:17 PM
  • Hi,

     If your website is configured to authenticate users with Forms Authentication ,

    and you want to access a Data Service hosted in the website from a windows client application,

    you will have to use Client Application Services to configure Forms Authentication as a WCF service, the links below should help you get started

    Brad Abrams : .NET Client Application Services
    Client Application Services

     

    and Gomata ,

    the context has a property called Credentials which you should use to go against Services which use Standard HTTP Authentication mechanisms,

     

    Code Snippet

    ServiceReference.NorthwindEntities context = new Client.ServiceReference.NorthwindEntities(new Uri(http://localhost:9999/service.svc));

    context.Credentials = System.Net.CredentialCache.DefaultCredentials;

     

     

    Friday, October 10, 2008 4:36 PM
    Moderator
  • hmm.  I could not get that method working.  I had to below.  When do you use which method?

     

    private SocialEntities GetDB()

    {

     Entities db = new Entities(restUri);

    //db.Credentials = System.Net.CredentialCache.DefaultCredentials;

    db.SendingRequest += new EventHandler<System.Data.Services.Client.SendingRequestEventArgs>(db_SendingRequest);

    return db;

    }

    private void db_SendingRequest(object sender, System.Data.Services.Client.SendingRequestEventArgs e)

    {

    // Grab the identity that ASP.NET membership set up for us

    ClientFormsIdentity aspNetIdentity = Thread.CurrentPrincipal.Identity as ClientFormsIdentity;

    // Copy the cookies

    if (aspNetIdentity != null)

    {

    // Grab the web request

    HttpWebRequest wr = e.Request as HttpWebRequest;

    wr.CookieContainer = aspNetIdentity.AuthenticationCookies;

    }

    }

    Friday, October 10, 2008 9:00 PM
  • HI

    Within my silverlight and DataServiceContext project , I want to send the authentication messages by the Http Header.
    but I found no way to modify the Header.
    and I don't found the "Credentials" property of
    DataServiceContext from my silverlight client,but I can find it from the server side;

    silverlight client side code where have no the Cradential property
    {
                System.Data.Services.Client.DataServiceContext content = new System.Data.Services.Client.DataServiceContext("");
               
                DataServiceContext svcCtx =
                       new DataServiceContext(
                           new Uri("Northwind.svc", UriKind.Relative));
                svcCtx.SendingRequest += new EventHandler<SendingRequestEventArgs>(svcCtx_SendingRequest);
    }

    and I don't fount it from the msdn library :
    DataServiceContext Members
    http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext_members%28VS.95%29.aspx
    please help me !

    It's very kind of you to give some simple code.
    N
    Thursday, August 6, 2009 10:33 AM
  • hmm.  I could not get that method working.  I had to below.  When do you use which method?

     

    private SocialEntities GetDB()

    {

     Entities db = new Entities (restUri);

    //db.Credentials = System.Net.CredentialCache.DefaultCredentials;

    db.SendingRequest += new EventHandler <System.Data.Services.Client. SendingRequestEventArgs >(db_SendingRequest);

    return db;

    }

    private void db_SendingRequest( object sender, System.Data.Services.Client. SendingRequestEventArgs e)

    {

    // Grab the identity that ASP.NET membership set up for us

    ClientFormsIdentity aspNetIdentity = Thread .CurrentPrincipal.Identity as ClientFormsIdentity ;

    // Copy the cookies

    if (aspNetIdentity != null )

    {

    // Grab the web request

    HttpWebRequest wr = e.Request as HttpWebRequest ;

    wr.CookieContainer = aspNetIdentity.AuthenticationCookies;

    }

    }


    ClientFormsIdentity     can not be used in silverlignt project..
    Tuesday, August 11, 2009 7:28 PM
  • Hi ,
     In Silverlight , using the Client Application services to authenticate your client browser will cause the Authentication cookie to be stored in the browser's cooke container.
     If you authenticate before you make the call to the DataService , then the authentication cookie will automatically get sent back to the server and this should work.
     Are you facing issues with authenticating with the web site from Silverlight ? or are you facing issues with sending the authentication cookie to the server ?
    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Thursday, August 13, 2009 5:01 PM
    Moderator
  • How do I specify CookieContainer for DataServiceContext?

    1) I use ClientHttp stack.

    2) I cannot read SessionId cookie from CookieContainer, since it it HttpOnly.

    3) I cannot access Request object from inside SendingRequest event, because there is not such property.

    4) I don't want to keep Credentials in memory longer than it is needed for authentication.

    Sunday, July 18, 2010 2:00 PM