Access ServiceStack session from ConnectionFilter

jklemmack

I am using SQL Server and database triggers to keep a data-level audit of all changes to the system. This audit includes the userID / name of whomever initiated a change. Ideally I'd like to do something like this in my AppHost.Configure method:

SqlServerDialect.Provider.UseUnicode = true;
var dbFactory = new OrmLiteConnectionFactory(ConnectionString, SqlServerDialect.Provider)
        {
            ConnectionFilter = (db =>
            {
                IAuthSession session = this.Request.GetSession();
                if (session != null && !session.UserName.IsNullOrEmpty())
                {
                    System.Data.IDbCommand cmd = db.CreateCommand();
                    cmd.CommandText = "declare @ci varbinary(128); select @ci = CAST(@Username as varbinary(128)); set context_info @ci";
                    System.Data.IDbDataParameter param = cmd.CreateParameter();
                    param.ParameterName = "Username";
                    param.DbType = System.Data.DbType.String;
                    //param.Value = session.UserName;
                    param.Value = session.UserAuthId;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                }

                return new ProfiledDbConnection(db, Profiler.Current);
            }),
            AutoDisposeConnection = true
        };
        container.Register<IDbConnectionFactory>(dbFactory);

Of course, this doesn't work because this.Request doesn't exist. Is there any way to access the current session from the ConnectionFilter or ExecFilter on an OrmLite connection?

The other approach I had started, doing an override of the Db property of Service, doesn't work any more because I've abstracted some activities into their own interfaced implementations to allow for mocks during testing. Each of these is passed a function that is expected to return the a DB connection. Example:

// Transaction processor
container.Register<ITransactionProcessor>(new MockTransactionProcessor(() => dbFactory.OpenDbConnection()));

So, how can I ensure that any DML executed has the (admittedly database-specific) context information needed for my database audit triggers?

mythz

The earlier multi tenant ServiceStack example shows how you can use the Request Context to store per-request items, e.g. you can populate the Request Context from a Global Request filter:

GlobalRequestFilters.Add((req, res, dto) =>
{
    var session = req.GetSession();
    if (session != null)
        RequestContext.Instance.Items.Add(
            "UserName", session.UserName);
});

And access it within your Connection Filter:

ConnectionFilter = (db =>
{
    var userName = RequestContext.Instance.Items["UserName"] as string;
    if (!userName.IsNullOrEmpty()) {
       //...
    }
}),

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access Session variable from iframe

From Dev

secure session cookie in servicestack

From Dev

ServiceStack update session on heartbeat

From Dev

Session based validation in ServiceStack

From Dev

secure session cookie in servicestack

From Dev

ServiceStack 'session' missing?

From Dev

How to read IHttpRequest to gain access to session in validation, on self-hosted ServiceStack?

From Dev

Access Flask session cookies from custom domain

From Dev

How to access Session data from database

From Dev

How to access session from DS.RESTAdapter?

From Dev

Access Symfony session values from external application

From Dev

How to access session from DS.RESTAdapter?

From Dev

Access subdomain session from main domain

From Dev

ServiceStack request parameter or session variable?

From Dev

ServiceStack: Accessing the session in InsertFilter and UpdateFilter

From Dev

Accessing Session in the ServiceStack Razor View

From Dev

Accessing Session in the ServiceStack Razor View

From Dev

ServiceStack: Accessing the session in InsertFilter and UpdateFilter

From Dev

ServiceStack session lifetime is increased on heartbeat

From Dev

Is it better to access data from an class object or from $_SESSION in PHP?

From Dev

ServiceStack ServerSentEvents restrict access to channel

From Dev

Meteor.js: access Session variable from RouteController waitOn

From Dev

How to access ASP classic session variable from PHP?

From Dev

How can I Access Session data from one bundle to another

From Dev

How to access wicket session from Jersey-2 request filter?

From Dev

Way to access from a repository to two values in session - Symfony2

From Dev

Is it possible to access HttpContext.Current.Session from Web API

From Dev

How to access session from a view in ASP .NET Core MVC 1.0

From Dev

How to access session from a custom RouteBase in MVC5?

Related Related

  1. 1

    Access Session variable from iframe

  2. 2

    secure session cookie in servicestack

  3. 3

    ServiceStack update session on heartbeat

  4. 4

    Session based validation in ServiceStack

  5. 5

    secure session cookie in servicestack

  6. 6

    ServiceStack 'session' missing?

  7. 7

    How to read IHttpRequest to gain access to session in validation, on self-hosted ServiceStack?

  8. 8

    Access Flask session cookies from custom domain

  9. 9

    How to access Session data from database

  10. 10

    How to access session from DS.RESTAdapter?

  11. 11

    Access Symfony session values from external application

  12. 12

    How to access session from DS.RESTAdapter?

  13. 13

    Access subdomain session from main domain

  14. 14

    ServiceStack request parameter or session variable?

  15. 15

    ServiceStack: Accessing the session in InsertFilter and UpdateFilter

  16. 16

    Accessing Session in the ServiceStack Razor View

  17. 17

    Accessing Session in the ServiceStack Razor View

  18. 18

    ServiceStack: Accessing the session in InsertFilter and UpdateFilter

  19. 19

    ServiceStack session lifetime is increased on heartbeat

  20. 20

    Is it better to access data from an class object or from $_SESSION in PHP?

  21. 21

    ServiceStack ServerSentEvents restrict access to channel

  22. 22

    Meteor.js: access Session variable from RouteController waitOn

  23. 23

    How to access ASP classic session variable from PHP?

  24. 24

    How can I Access Session data from one bundle to another

  25. 25

    How to access wicket session from Jersey-2 request filter?

  26. 26

    Way to access from a repository to two values in session - Symfony2

  27. 27

    Is it possible to access HttpContext.Current.Session from Web API

  28. 28

    How to access session from a view in ASP .NET Core MVC 1.0

  29. 29

    How to access session from a custom RouteBase in MVC5?

HotTag

Archive