Esempio n. 1
0
        public async Task Add(PersistedGrant token)
        {
            var entity = token.MapPersistedGrants();

            await using var con = new SqlConnection(_connectionString);
            await con.OpenAsync();

            await using var t = await con.BeginTransactionAsync();

            try
            {
                var ret = await con.ExecuteAsync($"insert into {_options.DbSchema}.PersistedGrants ([Key],ClientId,CreationTime,Data,Expiration,SubjectId,[Type]) values (@Key,@ClientId,@CreationTime,@Data,@Expiration,@SubjectId,@Type)", entity, commandType : CommandType.Text, transaction : t);

                if (ret != 1)
                {
                    throw new Exception($"Error inserting into PersistedGrants, return value is {ret}");
                }
                t.Commit();
            }
            catch (Exception ex)
            {
                t.Rollback();
                throw ex;
            }
        }
        public async Task Update(PersistedGrant token)
        {
            var dbGrant = await Get(token.Key);

            if (dbGrant == null)
            {
                throw new InvalidOperationException($"Can not find PersistedGrant with key={token.Key}.");
            }
            var entity = token.MapPersistedGrants();

            await using var con = new SqlConnection(_connectionString);
            await con.OpenAsync();

            await using var t = await con.BeginTransactionAsync();

            try
            {
                var ret = await con.ExecuteAsync($"update {_options.DbSchema}.PersistedGrants " +
                                                 "set ClientId = @ClientId," +
                                                 "[Data] = @Data, " +
                                                 "Expiration = @Expiration, " +
                                                 "SubjectId = @SubjectId, " +
                                                 "[Type] = @Type " +
                                                 "where [Key] = @Key", new
                {
                    entity.Key,
                    entity.ClientId,
                    entity.Data,
                    entity.Expiration,
                    entity.SubjectId,
                    entity.Type
                }, commandType : CommandType.Text, transaction : t);

                if (ret != 1)
                {
                    throw new Exception($"Error updating PersistedGrants, return value is  {ret}");
                }
                await t.CommitAsync();
            }
            catch (Exception)
            {
                await t.RollbackAsync();

                throw;
            }
        }