Example #1
0
        public async Task InvokeAsync(HttpContext context, Models.UserContext dbContext)
        {
            #region Task5
            // TODO:  Find if a header ‘ApiKey’ exists, and if it does, check the database to determine if the given API Key is valid
            //        Then set the correct roles for the User, using claims

            //string apiKey = context.Request.Headers["ApiKey"].FirstOrDefault(); //fix this statement (auth works, just need to grab apikey correctly)
            string apiKey = context.Request.Headers.FirstOrDefault(a => a.Key == "ApiKey").Value.ToString();

            var currentUser = UserDatabaseAccess.UserCheck_rObj(dbContext, apiKey);
            if (currentUser != null) //if user exists
            {
                var claimList = new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, currentUser.UserName),
                    new Claim(ClaimTypes.Role, currentUser.Role.ToString())
                };
                var userId = new ClaimsIdentity(claimList, apiKey);
                context.User.AddIdentity(userId);
            }
            #endregion

            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }
Example #2
0
        public IActionResult GetHello([FromHeader] string apiKey)
        {
            User currentuser = UserDatabaseAccess.UserCheck_rObj(_context, apiKey);

            return(StatusCode(200, "Hello " + currentuser.UserName));
        }