public IActionResult PutStartAutomatedTestingEC2(bool start) { try { StringValues accessToken = new StringValues(); Request.Headers.TryGetValue("Authorization", out accessToken); if (accessToken.Count() == 0) { return(StatusCode(401, "Empty or no authorization header.")); } if (accessToken.FirstOrDefault().ToString() == null || accessToken.FirstOrDefault().ToString() == "") { return(StatusCode(401, "Empty or no authorization header.")); } if (_validation.CheckValidation(accessToken.ToString())) { _ec2Logic.StartAutomatedTestingEC2(); return(Ok()); } return(StatusCode(403, "This is an invalid access token.")); } catch (Exception e) { _logger.LogError(e.Message, e); return(StatusCode(500)); } }
public IActionResult SendEmail([FromBody] Email email) { try { StringValues accessToken = new StringValues(); Request.Headers.TryGetValue("Authorization", out accessToken); if (accessToken.Count() == 0) { return(StatusCode(401, "Empty or no authorization header.")); } if (accessToken.FirstOrDefault().ToString() == null || accessToken.FirstOrDefault().ToString() == "") { return(StatusCode(401, "Empty or no authorization header.")); } if (_validation.CheckValidation(accessToken.ToString())) { _smtpLogic.SendEmail(email); return(Ok()); } return(StatusCode(403, "This is an invalid access token.")); } catch (Exception e) { _logger.LogError(e.Message, e); return(StatusCode(500, e.Message)); } }
public IActionResult GetInfo() { try { StringValues accessToken = new StringValues(); Request.Headers.TryGetValue("Authorization", out accessToken); if (accessToken.Count() == 0) { return(StatusCode(401, "Empty or no authorization header.")); } if (accessToken.FirstOrDefault().ToString() == null || accessToken.FirstOrDefault().ToString() == "") { return(StatusCode(401, "Empty or no authorization header.")); } if (_validation.CheckValidation(accessToken.ToString())) { return(Ok("Knights Arcade Restricted")); } return(StatusCode(403, "This is an invalid access token.")); } catch (Exception e) { _logger.LogError(e, e.Message); return(StatusCode(500, e.Message)); } }
public IActionResult DeleteArcadeMachine(int arcadeMachineId) { try { StringValues accessToken = new StringValues(); Request.Headers.TryGetValue("Authorization", out accessToken); if (accessToken.Count() == 0) { return(StatusCode(401, "Empty or no authorization header.")); } if (accessToken.FirstOrDefault().ToString() == null || accessToken.FirstOrDefault().ToString() == "") { return(StatusCode(401, "Empty or no authorization header.")); } if (_validation.CheckValidation(accessToken.ToString())) { _rdsLogic.DeleteArcadeMachine(arcadeMachineId); return(StatusCode(204)); } return(StatusCode(403, "This is an invalid access token.")); } catch (Exception e) { _logger.LogError(e.Message, e); return(StatusCode(500, e.Message)); } }
public IActionResult PostNewEntry([FromBody] NewEntry newEntry) { try { StringValues accessToken = new StringValues(); Request.Headers.TryGetValue("Authorization", out accessToken); if (accessToken.Count() == 0) { return(StatusCode(401, "Empty or no authorization header.")); } if (accessToken.FirstOrDefault().ToString() == null || accessToken.FirstOrDefault().ToString() == "") { return(StatusCode(401, "Empty or no authorization header.")); } if (_validation.CheckValidation(accessToken.ToString())) { Tuple <Games, int> tuple = _rdsLogic.PostNewEntry(newEntry); if (tuple.Item2 == 1) { return(StatusCode(409, "That game name already exists.")); } _ec2Logic.StartAutomatedTestingEC2(); return(StatusCode(201, tuple.Item1)); } return(StatusCode(403, "This is an invalid access token.")); } catch (Exception e) { _logger.LogError(e.Message, e); return(StatusCode(500, e.Message)); } }
public void OnAuthorization(AuthorizationFilterContext context) { StringValues token = ""; if (!context.HttpContext.Request.Headers.TryGetValue("Authorization", out token)) { context.Result = new UnauthorizedResult(); return; } if (token.Count() != 1) { context.Result = new UnauthorizedResult(); return; } var tokenStr = token.Single(); var authToken = _context.Tokens.Find(tokenStr); if (authToken == null) { context.Result = new UnauthorizedResult(); return; } if (authToken.Expires < DateTimeOffset.UtcNow.ToUnixTimeSeconds()) { context.Result = new UnauthorizedResult(); _context.Tokens.Remove(authToken); return; } }
private static JToken GetValue(StringValues strValues) { if (strValues.Count() == 1) { return(new JValue(strValues.First())); } return(new JArray(strValues)); }
public override void OnActionExecuting(ActionExecutingContext actionContext) { var authHeader = actionContext.HttpContext.Request.Headers.TryGetValue("Authorization", out xyz); var jwt = (xyz.Count() > 0 ? xyz.ToString().Split(" ")[1] : ""); actionContext.RouteData.Values.Add("jwtData", JwtManager.GetDataFromJWT(jwt)); base.OnActionExecuting(actionContext); }
public bool validateRequest(string email) { bool auth = false; string KeyAccess = string.Empty; Access access = new Access(); var path = _hostingEnvironment.ContentRootPath; // Or specify a specific name in a specific dir var MyIni = new IniFile(path + "\\auth.ini"); var mail = MyIni.Read("KeyAccess"); var pwd = MyIni.Read("KeySecret"); string authorization = string.Empty; //IEnumerable<string> headerValues = null; //HttpContext.Request.Headers.TryGetValue("Authorization", out headerValues); StringValues authValues = new StringValues(); HttpContext.Request.Headers.TryGetValue("Authorization", out authValues); var id = authValues.FirstOrDefault(); string SecretKey = authValues.ElementAt(authValues.Count() - 2); string signature = authValues.ElementAt(authValues.Count() - 1); //var credential = filterContext.HttpContext.Request.Headers["Authorization"].Substring(4).Split(':'); //var apiKey = Session.Query<ApiKey>().Where(k => k.AccessKey == credential[0]).FirstOrDefault(); if (SecretKey != null) { // check the date header is present & within 15 mins Encoding encoder = new UTF8Encoding(); HMACSHA1 signature2 = new HMACSHA1(encoder.GetBytes(SecretKey)); //string b64 = Convert.ToBase64String(signature2.ComputeHash(encoder.GetBytes(canonicalString.ToCharArray()))); if (signature2 == signature2) { auth = true; } } return(auth); }
public static bool TryRetrieveToken(HttpRequest request, out StringValues token, string [] roles) { //IEnumerable<string> authzHeaders; if (!request.Headers.TryGetValue("Authorization", out token) || token.Count() > 1) { return(false); } { var key = Encoding.ASCII.GetBytes("UmenderReddyAbbatiAttending CodingTest"); var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes("UmenderReddyAbbatiAttending CodingTest")); SecurityToken securityToken; JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); TokenValidationParameters validationParameters = new TokenValidationParameters() { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; string s = token.ToString().Replace("Bearer ", string.Empty); IPrincipal principal; // token validation principal = handler.ValidateToken(s, validationParameters, out securityToken); // Reading the "verificationKey" claim value: bool isAccess = false; foreach (string str in roles) { isAccess = principal.IsInRole(str); if (isAccess) { return(true); } else { continue; } } return(isAccess); } }
public void DefaultNullOrEmpty_ExpectedValues(StringValues stringValues) { Assert.Equal(0, stringValues.Count); Assert.Null((string)stringValues); Assert.Equal((string)null, stringValues); Assert.Equal(string.Empty, stringValues.ToString()); Assert.Equal(new string[0], stringValues.ToArray()); Assert.True(StringValues.IsNullOrEmpty(stringValues)); Assert.Throws<IndexOutOfRangeException>(() => stringValues[0]); Assert.Throws<IndexOutOfRangeException>(() => ((IList<string>)stringValues)[0]); Assert.Equal(string.Empty, stringValues.ToString()); Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(null)); Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(string.Empty)); Assert.Equal(-1, ((IList<string>)stringValues).IndexOf("not there")); Assert.False(((ICollection<string>)stringValues).Contains(null)); Assert.False(((ICollection<string>)stringValues).Contains(string.Empty)); Assert.False(((ICollection<string>)stringValues).Contains("not there")); Assert.Equal(0, stringValues.Count()); }