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

  • Question

  • 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 

    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

    Tuesday, April 16, 2013 11:42 AM

Answers

  • I've found the solution to my problem, i've added this entry to the server web.config

     <system.web.extensions>
        <scripting>
          <webServices>
            <authenticationService enabled="true" requireSSL="false" />
          </webServices>
        </scripting>
      </system.web.extensions>

    Wednesday, April 17, 2013 2:46 PM