C# Cookie Aware Webclient (Handles Login)

Toyz
by Toyz · 12 posts
11 years ago in .Net (C#, VB, etc)
Posted 11 years ago · Author
Ok I got bored while working on Mesh Thief and needed a method to login to IMVU website at any given time on the fly so I wrote a basic webclient h4c|< that dose this for me with given login information

CookieClient.cs
Code
public class CookieAwareWebClient : WebClient
    {
        private readonly CookieContainer _cookie = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = _cookie;
            }
            return request;
        }
    }


How to Use:
Code
var cCookie = new CookieAwareWebClient();
var loginData = new NameValueCollection { { "avatarname", "IMVU Username" }, { "password", "IMVU Password" } };
cCookie.UploadValues("https://secure.imvu.com/login/login/", "POST", loginData);


Simple and clean now you can keep reusing cCookit to download information from IMVUs site :D
Posted 9 years ago
good day, I looked at your code and I think this is what I am searching but how can I know if I am successfully login? because I tried it and I think it goes 50% right and 50% wrong because I dont have a confirmation if I successfully login. Sorry if I ask this simple question but for me its a big thing cause I am a beginner in coding. Hope you can help me with my problem.. thanks in advance!
Posted 9 years ago
quilab12 wrote:
good day, I looked at your code and I think this is what I am searching but how can I know if I am successfully login? because I tried it and I think it goes 50% right and 50% wrong because I dont have a confirmation if I successfully login. Sorry if I ask this simple question but for me its a big thing cause I am a beginner in coding. Hope you can help me with my problem.. thanks in advance!


Take a look at my thread here for an answer: https://www.imvumafias.org/community/vie ... 109&t=8503
Posted 9 years ago
am I getting the right track or not with the codes. [code=csharp file=Untitled.txt]using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer _cookie = new CookieContainer();

protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _cookie;
}
return request;
}
}
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
var cCookie = new CookieAwareWebClient();
var loginData = new NameValueCollection { { "loginname", "quilab12" }, { "password", "IMVU Password" } };
cCookie.UploadValues("https://secure.imvu.com/login/login/", "POST", loginData);
}
}
}
[/code]

and also I don't know where to put it this code [code=csharp file=Untitled.txt]string result = cCookie.DownloadString("https://www.imvumafias.org/community/index.php?sid=8df1cf70095142267021470e18879542");
foreach (Cookie IMVUCookie in result.Cookies)
{
if (IMVUCookie != null)
{
if (IMVUCookie.Name == "Logout [ quilab12 ]")
{
//success
}
else
{
//failure
}
}
}[/code]

I tried to put it at after "cCookie.UploadValues("https://secure.imvu.com/login/login/", "POST", loginData);" but it doesn't work



*edit: hahha.. I edit the code I post cause of some error.
Last edited by quilab12 on Wed Apr 30, 2014 6:00 am, edited 1 time in total.
Posted 9 years ago
quilab12 wrote:
and also I don't know where to put it this code [code=csharp file=Untitled.txt]string result = cCookie.DownloadString("http://stefantsov.com/");
foreach (Cookie IMVUCookie in result.Cookies)
{
if (IMVUCookie != null)
{
if (IMVUCookie.Name == "Logout [ quilab12 ]")
{
//success
}
else
{
//failure
}
}
}[/code]

I tried to put it at after "cCookie.UploadValues("https://secure.imvu.com/login/login/", "POST", loginData);" but it doesn't work


I don't know how you would validate the login using her code, I can't even get it to properly log into IMVU just using her example. It always fails and doesn't populate the cookie. The thread I linked you to is a completely different method for logging in that I use and works 100% of the time as long as you pass correct login information and supports validating the login.

It may be possible to splice the 2 methods together but I have no idea how and all my attempts at it have failed so far. If you figure it out, please share your code. I will keep trying myself and post my results.
Posted 9 years ago
sorry about the code I post. I edit it already but if I put it like this: string result = cCookie.DownloadString("https://www.imvumafias.org/community/index.php?sid=8df1cf70095142267021470e18879542"); I can't make it correctly.

dont worry If I figure it out how it will works, I will definitely share it.

-- Thu May 01, 2014 10:12 am --

Oh yeah.. I got it at last but in vb version

[code=vbnet file=Untitled.txt]Imports System.Net
Imports System.Collections.Specialized

Public Class Form1

Public Class CookieAwareWebClient
Inherits WebClient
Private ReadOnly _cookie As New CookieContainer()

Protected Overrides Function GetWebRequest(ByVal address As Uri) As WebRequest
Dim request As WebRequest = MyBase.GetWebRequest(address)
If TypeOf request Is HttpWebRequest Then
TryCast(request, HttpWebRequest).CookieContainer = _cookie
End If
Return request
End Function
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cCookie = New CookieAwareWebClient()
Dim loginData = New NameValueCollection() From { _
{"loginname", "quilab12"}, _
{"password", "*********"} _
}
cCookie.UploadValues("https://www.imvumafias.org/community/ucp.php?mode=login", "POST", loginData)

TextBox1.Text = cCookie.DownloadString("https://www.imvumafias.org/community/index.php")

If TextBox1.Text.Contains("icon-logout") Then
MsgBox("Success")
Else
MsgBox("failed")
End If
End Sub
End Class
[/code]
Posted 9 years ago
quilab12 wrote:
sorry about the code I post. I edit it already but if I put it like this: string result = cCookie.DownloadString("https://www.imvumafias.org/community/index.php?sid=8df1cf70095142267021470e18879542"); I can't make it correctly.

dont worry If I figure it out how it will works, I will definitely share it.

-- Thu May 01, 2014 10:12 am --

Oh yeah.. I got it at last but in vb version

[code=vbnet file=Untitled.txt]Imports System.Net
Imports System.Collections.Specialized

Public Class Form1

Public Class CookieAwareWebClient
Inherits WebClient
Private ReadOnly _cookie As New CookieContainer()

Protected Overrides Function GetWebRequest(ByVal address As Uri) As WebRequest
Dim request As WebRequest = MyBase.GetWebRequest(address)
If TypeOf request Is HttpWebRequest Then
TryCast(request, HttpWebRequest).CookieContainer = _cookie
End If
Return request
End Function
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cCookie = New CookieAwareWebClient()
Dim loginData = New NameValueCollection() From { _
{"loginname", "quilab12"}, _
{"password", "*********"} _
}
cCookie.UploadValues("https://www.imvumafias.org/community/ucp.php?mode=login", "POST", loginData)

TextBox1.Text = cCookie.DownloadString("https://www.imvumafias.org/community/index.php")

If TextBox1.Text.Contains("icon-logout") Then
MsgBox("Success")
Else
MsgBox("failed")
End If
End Sub
End Class
[/code]


Thanks, I will look this over and see if I can convert it to c#. Should be simple enough.

Edit: Alright, I sat down and looked over your code and did some testing. This method still won't for IMVU for me and your code is flawed. The "icon-logout" list element is going to exist whether or provide a valid login or not.
Posted 9 years ago
ah.. but it works fine with me. actually I tried it on another website and the result is a success also
Posted 9 years ago
quilab12 wrote:
ah.. but it works fine with me. actually I tried it on another website and the result is a success also


Have you tried using this on any of IMVU's apis that require authentication? For example, this api will always return null if you're not logged in: http://www.imvu.com/api/shop/product.php?pid=80

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT
Select a forum Protection     Help & Support     Introductions     Mafia News     IMVU News General Discussion     IMVU Lounge        IMVU Series / Roleplaying        Social Games     Mafia Market     Mafia Tools        Premium IMVU Tools        Off Topic Tools     Off Topic     Contests Creator Corner     Graphics Design        Photoshop        GIMP     Basic Creator Help     Catalog And Product Showcase     3D Meshing        3Ds Max        Sketchup        Blender Gangsters with Connections     White Hat Activities        Google Hacking        Trackers Programming Corner     Coding        Python        .Net (C#, VB, etc)        Flash        JAVA        Autoit        Batch        HTML & CSS        Javascript        PHP        Other        IMVU Homepage Codes           General           About me Panel           Messages Panel           Special Someone Panel           Visitors Panel           New Products Panel           Rankings Panel           Wishlist Panel           My Badges Panel           Outfits Panel           Url Panel           Groups Panel           Slideshow Panel           My Room Panel           Sandbox panel           Layouts     Help & Requests Free Credits     Approved Methods     Submit Methods Free Money     Approved Methods     Submit Methods Adult Corner     Get Mafia AP Here     AP Lounge        AP Social Games        Casual Dating Tips     IMVU Slave Market & Escorts