Search code examples
servicestackormlite-servicestack

Using ServiceStack Ormlite global filters with Session


I want to alter the example found here so that modifyby is populated with the session's username every time a change is made. Is this possible?


Solution

  • You would need singleton access to access the UserName which you would need to populate in either the ReqeustContext or ASP.NET's HttpContext.Current.Items Dictionary, e.g:

    GlobalRequestFilters.Add((req,res,dto) => {
        RequestContext.Instance.Items["UserName"] = req.GetSession().UserAuthName;
    });
    

    Which you can access from inside your UpdateFilter, e.g:

    OrmLiteConfig.UpdateFilter = (dbCmd, row) => {
        var auditRow = row as IAudit;
        var userName = RequestContext.Instance.Items["UserName"] as string;
        if (auditRow != null)
            auditRow.UserName = userName;
    };
    

    Note: the "UserName" will only work within the context of a HTTP Request.