locked
I need help! can't log in from the client side using WCF data service RRS feed

  • Question

  • User1289807499 posted

    Hey everyone, 

    i've been struggling with this issue for 3 days now.. i'm working with some colleagues in a training and this issue is making my work on stand by. let me explain to you the situation WCF Data services experts. 

    I have a web application forms hosted in IIS express, this app contains a wcf data service, and a model, this app is connected a database sql server 2012 express. in the other side i have a web application forms as a client consumer of the service. well the client must log in and do some operations like add users, other stuff.. but before they must to log in. 

    When i execute the client app and insert username/pass an webexception is thrown in the line: WebResponse res = request.GetResponse();

    heres the code of the log in button: 

    GlobalVariables.ctx = new XpoContext(GlobalVariables.serviceRootUri);
    GlobalVariables.ctx.Credentials = new NetworkCredential(UserName.Text, UserPass.Text);
    var res = GlobalVariables.ctx.GestionRestaurant_v3_Site.FirstOrDefault();
    FormsAuthentication.RedirectFromLoginPage(UserName.Text, Persist.Checked);

    in the web app i have a class XpoContext: 

    as i've mentionned before, the web application are hosted in IIS Express, i've created the virtual directory and copied/pasted the link in the xpocontext 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net;
    using System.Data.Services.Client;
    using System.IO;
    using System.Security;
    
    
    namespace KSclient.ServiceReference1
    {
        public partial class XpoContext
        {
    
            partial void OnContextCreated()
            {
                this.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestManualCookies);
            }
    
            //Using Manual Approach to Getting and Setting Cookies:
            void OnSendingRequestManualCookies(object sender, SendingRequestEventArgs e)
            {
                NetworkCredential creds = this.Credentials as NetworkCredential;
                string cookie = GetCookie(creds.UserName, creds.Password);
                e.RequestHeaders.Add("Cookie", cookie);
            }
    
            string _cookie;
            private string GetCookie(string username, string password)
            {
                if (_cookie == null)
                {
                    string loginServiceAddress =
                        string.Format("{0}/{1}/{2}",
                        "http://localhost:27796",
                        "Authentication_JSON_AppService.axd",
                        "Login");
                    string body = "";
                    WebRequest request =
                        CreateAuthRequest(username, password, loginServiceAddress, out body);
                    Stream stream = request.GetRequestStream();
                    StreamWriter sw = new StreamWriter(stream);
                    sw.Write(body);
                    sw.Close();
                    WebResponse res = request.GetResponse();
                    if (res.Headers["Set-Cookie"] != null)
                    {
                        _cookie = res.Headers["Set-Cookie"];
                    }
                    else
                    {
                        throw new SecurityException("Invalid username and password");
                    }
                }
                return _cookie;
            }
            private static WebRequest CreateAuthRequest(string username, string password, string loginServiceAddress, out string body)
            {
                WebRequest request = HttpWebRequest.Create(loginServiceAddress);
                request.Method = "POST";
                request.ContentType = "application/json";
                body = string.Format(
                    "{{ \"userName\": \"{0}\", \"password\": \"{1}\", \"createPersistentCookie\":false}}",
                    username,
                    password);
                request.ContentLength = body.Length;
                return request;
            }
    
    
    
    
        }
    }


    thank you for reading

    Monday, April 15, 2013 4:42 PM

Answers

  • User-1000095884 posted

    Hi,

    From your description, the issue is WCF data service related, I would suggest you post the issue in WCFdata service forum, there are more professionals on this issue, so you can get better support there. Thanks for your understanding and support.

    Best Regards.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 4:48 AM