public static async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = new StreamReader(req.Body).ReadToEnd(); Request <User> request = JsonConvert.DeserializeObject <Request <User> >(requestBody); User user = request.obj; string pass = request.TOKEN; // Generate TOKEN string AlphaNum = "abcdefghijklmnopqrstuvwxyz0123456789"; Random r = new Random(); char[] token = new char[50]; for (int i = 0; i < token.Length; i++) { token[i] = AlphaNum[r.Next(0, AlphaNum.Length - 1)]; } try { using (SqlConnection con = new SqlConnection(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING"))) { con.Open(); string sql = "Select id from Nutzer where nickname=@NAME"; using (SqlCommand com = new SqlCommand(sql, con)) { com.Parameters.Add("@NAME", System.Data.SqlDbType.NVarChar); com.Parameters["@NAME"].Value = user.name; using (SqlDataReader reader = com.ExecuteReader()) { if (reader.Read()) { request.Errorcode = -4; throw new Exception("Nutzer Bereits vorhanden"); } } } sql = "Insert Into Nutzer (nickname, pwd, blob) Values(@NAME, @PWD, @BLOB)"; using (SqlCommand com = new SqlCommand(sql, con)) { com.Parameters.Add("@NAME", System.Data.SqlDbType.NVarChar); com.Parameters.Add("@PWD", System.Data.SqlDbType.NVarChar); com.Parameters.Add("@BLOB", System.Data.SqlDbType.NVarChar); com.Parameters["@NAME"].Value = user.name; com.Parameters["@PWD"].Value = BCrypt.Net.BCrypt.HashPassword(user.name + pass, BCrypt.Net.BCrypt.GenerateSalt()); com.Parameters["@BLOB"].Value = user.name.ToLower() + new string(token); com.ExecuteNonQuery(); } int id; sql = "Select id from Nutzer where nickname=@NAME"; using (SqlCommand com = new SqlCommand(sql, con)) { com.Parameters.Add("@NAME", System.Data.SqlDbType.NVarChar); com.Parameters["@NAME"].Value = user.name; using (SqlDataReader reader = com.ExecuteReader()) { reader.Read(); id = reader.GetInt32(0); } } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING")); var client = storageAccount.CreateCloudBlobClient(); var cbContainer = client.GetContainerReference("images"); var container = client.GetContainerReference(user.name.ToLower() + new string(token)); await container.CreateIfNotExistsAsync(); //Blobs kopieren ISet <String> blobs = await FUNCTIONS2.GetImages(); await FUNCTIONS2.CopyImages(cbContainer, container, blobs); FUNCTIONS2.writeToDb(blobs, id, con); } } catch (SqlException e) { user.tempToken = null; request.Success = false; request.Error = e.Message; request.Errorcode = e.ErrorCode; return(new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(request)) }); } catch (Exception e) { user.tempToken = null; request.Success = false; request.Error = e.Message; return(new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(request)) }); } using (HttpClient http = new HttpClient()) { return(await http.PostAsync("https://projekt-gehirn-jogging-functions.azurewebsites.net/api/Login?code=Yued8GW/qIM7elGa/XcrSi4saQd5IQGS3atWGJ7VKh1Sz23X/jGT6Q==", new StringContent(JsonConvert.SerializeObject(request)))); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = new StreamReader(req.Body).ReadToEnd(); Request <ISet <String> > request = JsonConvert.DeserializeObject <Request <ISet <String> > >(requestBody); try { using (SqlConnection con = new SqlConnection(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING"))) { con.Open(); // TOKEN überprüfen HttpResponseMessage message = FUNCTIONS <ISet <String> > .checkToken(request, con); if (message != null) { return(message); } ISet <String> blobs = await FUNCTIONS2.GetImages(); string blob = null; string sql = "Select blob from Nutzer where id=" + request.Id; using (SqlCommand com1 = new SqlCommand(sql, con)) { using (SqlDataReader reader = com1.ExecuteReader()) { while (reader.Read()) { blob = reader.GetString(0); } } } sql = "Select * from imageNutzer where id_nutzer=" + request.Id; using (SqlCommand com1 = new SqlCommand(sql, con)) { using (SqlDataReader reader = com1.ExecuteReader()) { while (reader.Read()) { if (blobs.Contains(reader.GetString(1))) { blobs.Remove(reader.GetString(1)); } } } } if (blobs.Count == 0) { request.Errorcode = -11; throw new Exception(""); } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING")); var client = storageAccount.CreateCloudBlobClient(); var cbContainer = client.GetContainerReference("images"); var container = client.GetContainerReference(blob); //Blobs kopieren await FUNCTIONS2.CopyImages(cbContainer, container, blobs); FUNCTIONS2.writeToDb(blobs, request.Id, con); request.obj = blobs; } request.Success = true; } catch (SqlException e) { request.Success = false; request.Error = e.Message; request.Errorcode = e.ErrorCode; } catch (Exception e1) { request.Success = false; request.Error = "Ein Fehler ist aufgetreten"; } return(new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(request)) }); }