Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created June 20, 2016 16:06
Show Gist options
  • Save hikalkan/943a0eae1d9d5994c833daed81c463e9 to your computer and use it in GitHub Desktop.
Save hikalkan/943a0eae1d9d5994c833daed81c463e9 to your computer and use it in GitHub Desktop.
Creating an overridable session for ABP
using System;
using Abp.Runtime.Session;
namespace Abp.Temp
{
public static class AbpSessionExtensions
{
public static IDisposable Override(this IAbpSession abpSession, int? tenantId, long? userId)
{
var overridableSession = abpSession as OverridableSession;
if (overridableSession == null)
{
throw new ArgumentException("abpSession should be instance of OverridableSession", "abpSession");
}
return overridableSession.Override(tenantId, userId);
}
}
}
//Replace the session service in PreInitialize of your module
Configuration.ReplaceService<IAbpSession, OverridableSession>(DependencyLifeStyle.Singleton);
using Abp.Dependency;
using Abp.Runtime.Session;
namespace Abp.Temp
{
//TEST CODE
public class MyService : ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public MyService()
{
AbpSession = NullAbpSession.Instance;
}
public void Test1()
{
//...
using (AbpSession.Override(1,2))
{
//using IAbpSession will return tenantId = 1, userId = 2 until using statement ends.
}
//...
}
}
}
using System;
using System.Threading;
using Abp.Configuration.Startup;
using Abp.Runtime.Session;
namespace Abp.Temp
{
public class OverridableSession : ClaimsAbpSession
{
private AsyncLocal<SessionOverride> OverridedSession { get; set; }
public OverridableSession(IMultiTenancyConfig multiTenancy)
: base(multiTenancy)
{
OverridedSession = new AsyncLocal<SessionOverride>();
OverridedSession.Value = null;
}
public override long? UserId
{
get
{
if (OverridedSession.Value != null)
{
return OverridedSession.Value.UserId;
}
return base.UserId;
}
}
public override int? TenantId
{
get
{
if (OverridedSession.Value != null)
{
return OverridedSession.Value.TenantId;
}
return base.TenantId;
}
}
public override long? ImpersonatorUserId
{
get
{
if (OverridedSession.Value != null)
{
return OverridedSession.Value.ImpersonatorUserId;
}
return base.ImpersonatorUserId;
}
}
public override int? ImpersonatorTenantId
{
get
{
if (OverridedSession.Value != null)
{
return OverridedSession.Value.ImpersonatorTenantId;
}
return base.ImpersonatorTenantId;
}
}
public IDisposable Override(int? tenantId, long? userId)
{
OverridedSession.Value = new SessionOverride(tenantId, userId);
return new DisposeAction(() =>
{
OverridedSession.Value = null;
});
}
}
}
using Abp.MultiTenancy;
using Abp.Runtime.Session;
namespace Abp.Temp
{
public class SessionOverride : IAbpSession
{
public long? UserId { get; set; }
public int? TenantId { get; set; }
public long? ImpersonatorUserId { get; set; }
public int? ImpersonatorTenantId { get; set; }
public MultiTenancySides MultiTenancySide
{
get
{
return !TenantId.HasValue ? MultiTenancySides.Host : MultiTenancySides.Tenant;
}
}
public SessionOverride(int? tenantId, long? userId)
{
TenantId = tenantId;
UserId = userId;
}
}
}
@lommez
Copy link

lommez commented Aug 19, 2017

Hi Halil,
I tried to use this code in abp 2.3.0.
Looks like that isn´t compatible anymore.
Would you have a newer article of how to extend ClaimsAbpSession?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment