コード例 #1
0
ファイル: AspNetTokenDAL.cs プロジェクト: Hiblet/IdentityExp1
        public void Insert(ApplicationJwtRefreshToken token)
        {
            string prefix = nameof(Insert) + Constants.FNSUFFIX;

            if (!IsSqlSanitizeValid(token.Name, MAXSIZE_USERNAME))
            {
                throw new ArgumentException($"User name [{token.Name}] does not pass SQL Sanitization.");
            }

            StringBuilder sb = new StringBuilder();

            sb.Append($"INSERT INTO {_table} (");

            sb.Append("UpdateTimestamp,");
            sb.Append("IP,");
            sb.Append("Name,");
            sb.Append("Guid");

            sb.Append(") VALUES (");

            sb.Append($"{GetDate()},");
            sb.Append($"{SqlizeNoSanitize(token.IP)},");
            sb.Append($"{Sqlize(token.Name)},");
            sb.Append($"{SqlizeNoSanitize(token.Guid)}");

            sb.Append(");");

            ExecNonQuery(sb.ToString(), prefix);
        }
コード例 #2
0
        public Task InsertTokenAsync(ApplicationJwtRefreshToken token, CancellationToken cancellationToken)
        {
            string prefix = nameof(InsertTokenAsync) + Constants.FNSUFFIX;

            _tokens.CreateAsync(token, cancellationToken);

            return(Task.CompletedTask);
        }
コード例 #3
0
        public Task InsertTokenAsync(ApplicationJwtRefreshToken token, CancellationToken cancellationToken)
        {
            string prefix = nameof(InsertTokenAsync) + Constants.FNSUFFIX;

            try { using (var tokensDAL = new AspNetTokensDAL(_connStr)) { tokensDAL.Insert(token); } }
            catch (Exception ex) { _logger.LogError(prefix + $"Exception:[{ex.ToString()}]"); }

            return(Task.CompletedTask);
        }
コード例 #4
0
        public Task <ApplicationJwtRefreshToken> ExtractTokenAsync(string guid, CancellationToken cancellationToken)
        {
            string prefix = nameof(ExtractTokenAsync) + Constants.FNSUFFIX;

            ApplicationJwtRefreshToken token = _tokens.FindByGuidAsync(guid, cancellationToken).Result;

            if (token != null)
            {
                _tokens.DeleteAsync(guid, cancellationToken);
            }

            return(Task.FromResult(token));
        }
コード例 #5
0
        public Task <ApplicationJwtRefreshToken> ExtractTokenAsync(string guid, CancellationToken cancellationToken)
        {
            string prefix = nameof(ExtractTokenAsync) + Constants.FNSUFFIX;

            ApplicationJwtRefreshToken token = null;

            try
            {
                using (var tokensDAL = new AspNetTokensDAL(_connStr))
                {
                    token = tokensDAL.SelectByGuid(guid);
                    if (token != null)
                    {
                        tokensDAL.Delete(guid);
                    }
                }
            }
            catch (Exception ex) { _logger.LogError(prefix + $"Exception:[{ex.ToString()}]"); }

            return(Task.FromResult(token));
        }
コード例 #6
0
ファイル: AspNetTokenDAL.cs プロジェクト: Hiblet/IdentityExp1
        public ApplicationJwtRefreshToken SelectByColumnValue(string column, string colValue)
        {
            string prefix = nameof(SelectByColumnValue) + Constants.FNSUFFIX;

            if (string.IsNullOrWhiteSpace(colValue))
            {
                throw new ArgumentNullException(nameof(colValue));
            }
            if (string.IsNullOrWhiteSpace(column))
            {
                throw new ArgumentNullException(nameof(column));
            }

            List <string> cols = new List <string>();

            cols.Add("Name");
            cols.Add("IP");
            cols.Add("Guid");
            string csvCols = string.Join(",", cols);

            string sql = $"SELECT {csvCols} FROM {_table} WHERE {column}={SqlizeNoSanitize(colValue)};";

            int countRecord = 0;
            List <ApplicationJwtRefreshToken> loadedRecords = new List <ApplicationJwtRefreshToken>();

            try
            {
                using (DbCommand cmd = CreateCmd(sql))
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ++countRecord;

                            string ip   = "";
                            string guid = "";
                            string name = "";

                            if (reader["IP"] != DBNull.Value)
                            {
                                ip = (string)reader["IP"];
                            }
                            if (reader["Guid"] != DBNull.Value)
                            {
                                guid = (string)reader["Guid"];
                            }
                            if (reader["Name"] != DBNull.Value)
                            {
                                name = (string)reader["Name"];
                            }

                            ApplicationJwtRefreshToken token = new ApplicationJwtRefreshToken
                            {
                                Name = name,
                                Guid = guid,
                                IP   = ip
                            };

                            loadedRecords.Add(token);
                        } // end of while...
                    }// end of using...
            }
            catch (Exception ex)
            {
                string msg = $"Failed to find record where {column}={SqlizeNoSanitize(colValue)}; Exception=[{ex.ToString()}]";
                Log4NetAsyncLog.Error(prefix + msg);
            }

            int countLoadedRecords = loadedRecords.Count();

            if (countLoadedRecords == 1)
            {
                return(loadedRecords.First());
            }

            if (countLoadedRecords > 1)
            {
                string msg = $"More than one record exists where {column}={SqlizeNoSanitize(colValue)}; This should never happen.";
                Log4NetAsyncLog.Error(prefix + msg);
                return(loadedRecords.FirstOrDefault());
            }

            // Zero case
            return(null);
        }
コード例 #7
0
 public Task AddTokenAsync(ExampleApplicationUser user, ApplicationJwtRefreshToken token, CancellationToken cToken)
 {
     return(_tokens.CreateAsync(token, cToken));
 }