I'm working with one of user controlled application. This app is connecting with PHP files to login and do any other processes that requested.
in PHP I'm using session for detect if user already login. When they login successfully php sets and send login token id successfully.
I already working with IOS for same project and in IOS login process is working fine. IOS uses cookies automatically but I couldn't use cookies for windows app. Session has named cookie token id and php file checks token id with posted. I stuck in windows but I have no problem with IOS.
In windows store and phone app I can post perfectly and I can get respond message too. I can login perfectly as well. But when I navigate to second page of app it checks we are login or not again. When it check I'm facing with not logged in message. In my IOS app run perfectly. But in windows session token id check fails..
this is my php file that checks session token id in second page:
$tokencheck = $_POST['Tokenid'];
if($_SESSION["token"] == $tokencheck){
first page is login page and when login successfull windows phone gets token id and saves it successfully. After login, in a second page saved token id posting to php that I shared. I checked token id and thats true.
I do some research and I found problem is cookies. I can't use cookies right now. I did some more codes but still I stuck and couldn't solved this problem.
Codes sends post perfectly and also gets respond messages perfectly but I couldn't check session token id, this is because login check fails.
First page, first attempt with httpClient
var values = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", txtEmail.Text),
new KeyValuePair<string, string>("Password", txtPassword.Password)
};
string url = ".../login.php";
CookieContainer Cookiejar = new CookieContainer();
var handler = new HttpClientHandler
{
CookieContainer = Cookiejar,
UseCookies = true//,
// UseDefaultCredentials = false
};
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var wwwUri = new Uri(url);
Cookiejar.SetCookies(wwwUri, Cookiejar.GetCookieHeader(wwwUri));
First page second attempt with HttpWebRequest
CookieContainer cookies = new CookieContainer();
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
getRequest.CookieContainer = cookies;
getRequest.Method = "POST";
HttpWebResponse response = (HttpWebResponse)await getRequest.GetResponseAsync();
var sr = new StreamReader(response.GetResponseStream() );
string responseString = sr.ReadToEnd();
I also tried some other codes that I found in internet but can't solved yet.
Second page is also :
object lgntoken = Windows.Storage.ApplicationData.Current.LocalSettings.Values["logintokenid"];
var values = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Tokenid", Convert.ToString(lgntoken))
};
string url = ".../get_projects_list.php";
CookieContainer Cookiejar = new CookieContainer();
var wwwUri = new Uri(url);
// Cookiejar.SetCookies(wwwUri, Cookiejar.GetCookieHeader(wwwUri));
Cookiejar.GetCookieHeader(wwwUri);
var handler = new HttpClientHandler
{
CookieContainer = Cookiejar,
UseCookies = true
};
System.Diagnostics.Debug.WriteLine("Login Token: " + Convert.ToString(lgntoken) + "..");
var httpClient = new HttpClient(handler);
HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine("Project List Data : " + responseString + " ++ Login Token: " + Convert.ToString(lgntoken) + "..");
I have some experiance in IOS but I'm newbie in windows store apps. I stuck about this issue and already not understood what should I do, how can I set or get cookies in a first and second page. Waiting your helps, thank you.