/// <summary> /// Authorization request header from client. /// </summary> /// <param name="header">header from client</param> /// <returns>-401/-403/{userId}</returns> public static long Authentication(HttpRequestHeaders header) { string authorization = header.GetValues("Authorization").FirstOrDefault(); if (authorization == null) { return -401; } using (var db = new CF_FamsamEntities()) { string token = authorization.Split(null)[1]; Session session = db.Session.Find(token); Debug.WriteLine("____________________________" + session.token); if (session == null) return -403; if (session.expired < DateTime.Now) { Debug.WriteLine("____________________________ session mili:" + session.expired.Millisecond); Debug.WriteLine("____________________________ now mili:" + DateTime.Now.Millisecond); //session expired db.Session.Remove(session); db.SaveChanges(); return -403; } else { return session.User.id; } } }
private static string ExtractAuthHeader(System.Net.Http.Headers.HttpRequestHeaders headers, string headerName) { string result = String.Empty; try { //Check header result = String.IsNullOrEmpty(headers.GetValues(headerName).FirstOrDefault()) ? String.Empty : headers.GetValues(headerName).FirstOrDefault(); } catch (Exception) { //Nothing, the header was not found } if (String.IsNullOrEmpty(result)) { try { //Check Cookie var cookieAuth = GetCookie(headers, headerName); if (cookieAuth != null) { result = cookieAuth; } } catch { // fallback here because it is null } } return(result); }
private string _GetCanonicalizedHeaders(HttpRequestHeaders headers) { var orderedHeaders = headers.OrderBy(x => x.Key); var headersWithAggregatedValues = orderedHeaders.Where(x => x.Key.StartsWith("x-ms")).Select(x => x.Key.ToLowerInvariant() + ":" + headers.GetValues(x.Key).Aggregate((x1, x2) => x1 + "," + x2)); var canonicalHeader = headersWithAggregatedValues.Aggregate((x1, x2) => x1 + "\n" + x2) + "\n"; return canonicalHeader; }
public static bool AuthenticateUser(HttpRequestHeaders HttpHeaders) { if (HttpHeaders.Contains(HttpRequestHeaderName)) { var authHeader = HttpHeaders.GetValues(HttpRequestHeaderName).First(); return _authenticateHeaderValue(authHeader); } return false; }
public static int? ParseFirstValue(string header, HttpRequestHeaders headers) { IEnumerable<string> value = (headers.Contains(header) ? headers.GetValues(header) : null); return GetIntValue(value); }
public static string GetHeaderValue(string header, HttpRequestHeaders headers) { IEnumerable<string> value = (headers.Contains(header) ? headers.GetValues(header) : null); return GetStringValue(value); }
internal static IEnumerable<string> GetHeaderValues(HttpRequestHeaders headers, string headerName) { var list = new List<string>(); var values = headers.GetValues(headerName); if (values != null) { list.AddRange(values.Select(value => value.TrimStart(new char[0]))); } return list; }
private AuthorizationComponents TakeHeaderData(HttpRequestHeaders headers) { string key = "", hash = "", dateTimeSent = ""; if (headers.Contains("Key")) key = headers.GetValues("key").First(); else { throw new NoKeyProvidedException(); } if (headers.Contains("Hash")) hash = headers.GetValues("hash").First(); else { throw new NoHashProvidedEception(); } if (headers.Contains("DateSent")) dateTimeSent = headers.GetValues("DateSent").First(); else { throw new NoDateProvidedException(); } if (String.IsNullOrWhiteSpace(key)) { throw new InvalidHeaderException("key"); } if(String.IsNullOrWhiteSpace(hash)) { throw new InvalidHeaderException("hash"); } if(String.IsNullOrWhiteSpace(dateTimeSent)) { throw new InvalidHeaderException("date"); } DateTime sent = DateTime.Parse(dateTimeSent, this.EndUserDateFormat, DateTimeStyles.AssumeUniversal); return new AuthorizationComponents { PublicKey = key, DataHash = hash, TimeRequestExecuted = sent }; }