How can I check whether the login is successful, or failed, is it something with cookies?
Here’s my code:
const string baseurl = "http://maplestory.nexon.net/Default.aspx?PART=/Main";
CookieContainer cookie;
public Login(string user, string password)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string login = string.Format("userID={0}&password={1}&device_id={2}", user, password, "24275770-88e0-4e");
byte[] postbuf = Encoding.ASCII.GetBytes(login);
req.ContentLength = postbuf.Length;
Stream rs = req.GetRequestStream();
rs.Write(postbuf, 0, postbuf.Length);
rs.Close();
cookie = req.CookieContainer = new CookieContainer();
WebResponse resp = req.GetResponse();
resp.Close();
}
}
I don’t know if I really logged in. and BTW is there anything wrong with the current code?
Thank you very much.
In your login logic, you usually send a cookie back in the response with some sort of token that you can check on each subsequent request.
So in your code, you’d create a authentication cookie and add it to your response:
Take a look at how Forms Authentication works in ASP.NET, along with using a MembershipProvider to handle your authentication. I’m not a huge fan of MembershipProvider, but it does provide a lot of basic authentication wiring with not a lot of effort (especially if you’re cool with the default AD or SQL providers for membership).
Hope this helps!
UPDATE If I wanted to check if a user is already logged in, I would need to check their Cookies collection and see if they have a valid authentication token/cookie. If you use forms authentication, this is done for you. If you don’t have a valid cookie, you’ll be redirected back to the login page.