コード例 #1
0
        public async Task <bool> RemoveRefreshToken(int userId)
        {
            bool isTokenRemoved = false;

            try
            {
                using (SqlConnection cn = new SqlConnection(this.ConnectionString))
                    using (SqlCommand cmd = new SqlCommand(RefreshTokenQueries.DeleteRefreshToken(), cn))
                    {
                        cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
                        await cn.OpenAsync();

                        await cmd.ExecuteNonQueryAsync();

                        cn.Close();
                    }
                isTokenRemoved = true;
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex.Message);
                throw ex;
            }
            return(isTokenRemoved);
        }
コード例 #2
0
        public async Task <UserRefreshToken> GetRefreshTokenByUserId(int userId)
        {
            UserRefreshToken userRefreshToken = new UserRefreshToken();

            try
            {
                using (SqlConnection cn = new SqlConnection(this.ConnectionString))
                    using (SqlCommand cmd = new SqlCommand(RefreshTokenQueries.SelectRefreshTokenByUserId(), cn))
                    {
                        cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
                        await cn.OpenAsync();

                        SqlDataReader reader = await cmd.ExecuteReaderAsync();

                        while (await reader.ReadAsync())
                        {
                            userRefreshToken.Id           = Convert.ToInt32(reader["Id"]);
                            userRefreshToken.UserId       = Convert.ToInt32(reader["UserId"]);
                            userRefreshToken.RefreshToken = reader["RefreshToken"].ToString();
                        }
                    }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex.Message);
                throw ex;
            }
            if (string.IsNullOrWhiteSpace(userRefreshToken.RefreshToken))
            {
                return(null);
            }

            return(userRefreshToken);
        }