public Login_process(string usernaam, string password)
        {
            try
            {
                var request    = new Request();
                var loginModel = new login_request {
                    username = usernaam, password = password
                };

                response = (login_response)request.Execute <login_response>(Rest_API.serverurl + "/api/users/login", loginModel, "POST");

                if (response.status != 200)
                {
                    MaterialMessageBox.Show(response.message);
                }

                if (response.status == 200)
                {
                    login_response login_ = new login_response();
                    login_.id        = response.id;
                    login_.name      = response.name;
                    login_.email     = response.email;
                    login_.username  = response.username;
                    login_.role      = response.role;
                    login_.status    = response.status;
                    login_.message   = response.message;
                    login_.FCM_token = response.FCM_token;
                    user_info.Add(login_);

                    if (response.role == "admin")
                    {
                        //Mainpageadmin UI_Mainpage_admin = new Mainpageadmin();
                        Mainpage_normaal UI_mainpage = new Mainpage_normaal();
                        UI_mainpage.Show();
                    }
                    if (response.role == "normal_user")
                    {
                        //Mainpage UI_mainpage = new Mainpage();
                        Mainpage_normaal UI_mainpage = new Mainpage_normaal();
                        UI_mainpage.Show();
                    }
                }
                return;
            }

            catch (Exception ex)
            {
                string error = ex.ToString();

                MaterialMessageBox.Show(error);
            }
        }
示例#2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            // Parse query parameter
            string email = req.GetQueryNameValuePairs()
                           .FirstOrDefault(q => string.Compare(q.Key, "email", true) == 0)
                           .Value;
            string password = req.GetQueryNameValuePairs()
                              .FirstOrDefault(q => string.Compare(q.Key, "password", true) == 0)
                              .Value;
            string notification_token = req.GetQueryNameValuePairs()
                                        .FirstOrDefault(q => string.Compare(q.Key, "token", true) == 0)
                                        .Value;

            // Task vars
            string _conn_str     = System.Environment.GetEnvironmentVariable("sqldb_connection");
            bool   login_success = false;

            // Get user from the DB
            int    user_id    = 0;
            string input_pass = SHA.GenerateSHA256String(password).ToLower(); // Encrypc input_password (SHA256)
            string query      = "SELECT top 1 password_enc, enc_string, id FROM Users WHERE email = @email";

            login_response res = new login_response(-1, 0, null); // Default bad login response

            using (SqlConnection conn = new SqlConnection(_conn_str)) {
                conn.Open();
                SqlCommand get_cmd = new SqlCommand(query, conn);
                get_cmd.Prepare();
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@email";
                param.Value         = email;
                get_cmd.Parameters.Add(param);
                string enc_string = utilitles.RandomString(5); // Generate a new enc_string.
                using (SqlDataReader reader = get_cmd.ExecuteReader()) {
                    if (reader.Read())
                    {
                        if (string.Compare(input_pass, (string)reader["password_enc"]) == 0)   // Checks if the 2 encrypted passwords match.
                        {
                            login_success = true;
                            user_id       = (int)reader["id"];
                            string login_hash = SHA.GenerateSHA256String(input_pass + enc_string).ToLower(); // Generate login hash.
                            res = new login_response(1, (int)reader["id"], login_hash);                      // Generate login response.
                        }
                    }
                }
                if (login_success)   // Update the new enc_string in DB.
                {
                    string     update_enc_string = "UPDATE Users SET enc_string = '" + enc_string + "' WHERE email = '" + email + "'";
                    SqlCommand update_cmd        = new SqlCommand(update_enc_string, conn);
                    update_cmd.ExecuteNonQuery();
                    string     update_token_query = "UPDATE Users SET notification_token = @notification_token WHERE id = @user_id";
                    SqlCommand update_token_cmd   = new SqlCommand(update_token_query, conn);
                    update_token_cmd.Prepare();
                    SqlParameter param_token = new SqlParameter();
                    param_token.ParameterName = "@notification_token";
                    param_token.Value         = notification_token;
                    update_token_cmd.Parameters.Add(param_token);
                    SqlParameter param_uid = new SqlParameter();
                    param_uid.ParameterName = "@user_id";
                    param_uid.Value         = user_id;
                    update_token_cmd.Parameters.Add(param_uid);
                    update_token_cmd.ExecuteNonQuery();
                }
                conn.Close();
            }

            return(req.CreateResponse(HttpStatusCode.OK, res, JsonMediaTypeFormatter.DefaultMediaType));
        }
示例#3
0
        public async System.Threading.Tasks.Task <IEnumerable <login_response> > Post()
        {
            Post_Request_Struct post_request_struct;

            post_request_struct.userid   = null;
            post_request_struct.password = null;

            try
            {
                System.IO.Stream dataStream0 = await this.Request.Content.ReadAsStreamAsync();

                // Open the stream using a StreamReader for easy access.
                //dataStream0.Seek(0, System.IO.SeekOrigin.Begin);
                System.IO.StreamReader reader0 = new System.IO.StreamReader(dataStream0);
                // Read the content.
                string temp = reader0.ReadToEnd();
                //System.Console.Write ($"temp {temp}");
                post_request_struct = Newtonsoft.Json.JsonConvert.DeserializeObject <Post_Request_Struct> (temp);

                //mmria.server.util.LuceneSearchIndexer.RunIndex(new List<mmria.common.model.home_record> { mmria.common.model.home_record.convert(queue_request)});
                //System.Dynamic.ExpandoObject json_result = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(result, new  Newtonsoft.Json.Converters.ExpandoObjectConverter());



                //string metadata = DecodeUrlString(temp);
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }


            /*
             * HOST="http://127.0.0.1:5984"
             * > curl -vX POST $HOST/_session -H 'Content-Type: application/x-www-form-urlencoded' -d 'name=anna&password=secret'
             */
            try
            {
                string post_data       = string.Format("name={0}&password={1}", post_request_struct.userid, post_request_struct.password);
                byte[] post_byte_array = System.Text.Encoding.ASCII.GetBytes(post_data);


                //string request_string = "http://*****:*****@localhost:5984/_session";
                string request_string         = Program.config_couchdb_url + "/_session";
                System.Net.WebRequest request = System.Net.WebRequest.Create(new Uri(request_string));
                //request.UseDefaultCredentials = true;

                request.PreAuthenticate = false;
                //request.Credentials = new System.Net.NetworkCredential("mmrds", "mmrds");
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = post_byte_array.Length;

                using (System.IO.Stream stream = request.GetRequestStream())
                {
                    stream.Write(post_byte_array, 0, post_byte_array.Length);
                }                /**/

                System.Net.WebResponse response = await request.GetResponseAsync();

                System.IO.Stream dataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();

                login_response json_result = Newtonsoft.Json.JsonConvert.DeserializeObject <login_response>(responseFromServer);

                login_response[] result = new login_response[]
                {
                    json_result
                };


                string[] set_cookie = response.Headers["Set-Cookie"].Split(';');
                string[] auth_array = set_cookie[0].Split('=');
                if (auth_array.Length > 1)
                {
                    string auth_session_token = auth_array[1];
                    result[0].auth_session = auth_session_token;
                }
                else
                {
                    result[0].auth_session = "";
                }

                //this.ActionContext.Response.Headers.Add("Set-Cookie", auth_session_token);

                return(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(null);
        }