public AudienceDto Get(string id)
        {
            AudienceDto audience = null;

            using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
            {
                using (SqlCommand sqlCommand = new SqlCommand("GetAudience", sqlConnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;

                    sqlConnection.Open();

                    using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (sqlDataReader.Read())
                        {
                            string name = sqlDataReader.GetString(sqlDataReader.GetOrdinal("Name"));

                            audience = new AudienceDto()
                            {
                                Id = id.ToString(), Name = name
                            };
                        }

                        return(String.IsNullOrEmpty(audience.Id) ? null : audience);
                    }
                }
            }
        }
        public async Task <Audience> AddAudience(AudienceDto doc)
        {
            var clientId = Guid.NewGuid().ToString("N");
            var key      = new byte[32];

            RandomNumberGenerator.Create().GetBytes(key);
            var base64Secret = Convert.ToBase64String(key);
            var newAudience  = new Audience()
            {
                ClientId     = clientId,
                Base64Secret = base64Secret,
                Name         = doc.Name,
                Issuer       = doc.Issuer
            };

            await AuthDbConnection.bakserverWebBoardAuthConn.OpenAsync();

            string     sql = "INSERT INTO Audiences(ClientId,Base64Secret,Name,Issuer) VALUES(@param1,@param2,@param3,@param4)";
            SqlCommand cmd = new SqlCommand(sql, AuthDbConnection.bakserverWebBoardAuthConn);

            cmd.Parameters.Add("@param1", SqlDbType.VarChar, 32).Value  = newAudience.ClientId;
            cmd.Parameters.Add("@param2", SqlDbType.VarChar, 80).Value  = newAudience.Base64Secret;
            cmd.Parameters.Add("@param3", SqlDbType.VarChar, 100).Value = newAudience.Name;
            cmd.Parameters.Add("@param4", SqlDbType.VarChar, 100).Value = newAudience.Issuer;
            cmd.CommandType = CommandType.Text;

            await cmd.ExecuteNonQueryAsync();

            AuthDbConnection.bakserverWebBoardAuthConn.Close();

            return(newAudience);
        }
        public void Create(AudienceDto audience)
        {
            using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
            {
                using (SqlCommand sqlCommand = new SqlCommand("AddAudience", sqlConnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    sqlCommand.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = audience.Name;

                    sqlConnection.Open();
                    sqlCommand.ExecuteNonQuery();
                }
            }
        }
Beispiel #4
0
        // api/Audience/Post
        public async Task <object> Post(AudienceViewModel audienceModel)
        {
            audienceModel = dmmy();
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var audienceDto = new AudienceDto();

            audienceDto.Name   = audienceModel.Name;
            audienceDto.Issuer = audienceModel.Issuer;
            var newAudience = await LogicUnitOfWork.AudienceService.AddAudience(audienceDto);

            return(Ok(newAudience));
        }