Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC

Activities of "alexmann_petal"

@ismcagdas That worked perfectly! Thank you

We are trying to use RLS on a second database connection (not the abp DB). To get the RLS to work we need to inject a session context to the SQL procedure call each time entity framework calls the database. The session context we want to inject is the username (or UserID it doesn’t really matter which).
We are able to inject the session context using the below code in the DbContextConfigurer

public class PETALDataDbContextConfigurer: ITransientDependency
{
        private static SqlConnection connection;

public static void Configure(DbContextOptionsBuilder<PETALDataDbContext> builder, string connectionString)
        {

            connection = new SqlConnection(connectionString);
            connection.StateChange += Connection_StateChange;

            builder.UseSqlServer(connection);
        }

public static void Configure(DbContextOptionsBuilder<PETALDataDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection);
        }

private static void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
        {

            var username = "need username";
            if (e.CurrentState == ConnectionState.Open)
            {
                var cmd = connection.CreateCommand();
cmd.CommandText = @"exec sp_set_session_context @key=N'Username', @value='" + username + "'";
cmd.ExecuteNonQuery();
            }
        }
}

The problem is getting the username at this point. Have referred to this documentation a lot https://aspnetboilerplate.com/Pages/Documents/Abp-Session But haven’t been able to get an instantiated AbpSession. Is there another way to get the username that will work at this point? In case its not clear from the code the name of the second database is PETALData.

Showing 1 to 2 of 2 entries