Ejemplo n.º 1
0
        public async Task <ActionResult <User> > DeleteUser([FromHeader] string APIKey, [FromQuery] string userName)
        {
            try
            {
                if (await UserDatabaseAccess.APIKeyExists(APIKey, base._context))
                {
                    var user = await UserDatabaseAccess.GetUser(APIKey, base._context);

                    #region Registering UserLog
                    string Method    = this.HttpContext.Request.Method;
                    string Path      = this.HttpContext.Request.Path;
                    var    logString = user.Role + " requested " + Method + " " + Path;

                    await UserDatabaseAccess.AddLog(logString, user, base._context);

                    #endregion

                    if (user.ApiKey == APIKey && user.UserName == userName)
                    {
                        await UserDatabaseAccess.BackupLog(APIKey, base._context);

                        await UserDatabaseAccess.RemoveUser(user, base._context);

                        return(StatusCode(200, true));
                    }
                }
                throw new Exception();
            }
            catch (Exception)
            {
                return(StatusCode(200, false));
            }
        }
Ejemplo n.º 2
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

            var AuthAPIKey = context.Request.Headers["APIKey"].ToString();
            var verifyKey  = await UserDatabaseAccess.APIKeyExists(AuthAPIKey, dbContext);

            if (verifyKey)
            {
                var user = await UserDatabaseAccess.GetUser(AuthAPIKey, dbContext);

                if (user != null)
                {
                    var claimArray = new Claim[]
                    {
                        new Claim(ClaimTypes.Name, user.UserName),
                        new Claim(ClaimTypes.Role, user.Role)
                    };

                    context.User.AddIdentity(new ClaimsIdentity(claimArray, "APIKey"));
                }
            }
            #endregion

            // Call the next delegate/middleware in the pipeline

            await _next(context);
        }