using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Abp.Dependency;
using DinePlan.DineConnect.Authorization.Users;
using Microsoft.Owin.Security;
namespace DinePlan.DineConnect.Session
{
public class ConnectSession : ITransientDependency
{
public ConnectSession()
{
var test = "";
}
public int OrgId
{
get
{
var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
var tenantIdClaim = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type.Equals( "Organization"));
if (!string.IsNullOrEmpty(tenantIdClaim?.Value))
{
return Convert.ToInt32(tenantIdClaim.Value);
}
return 0;
}
set
{
var currentPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
var identity = currentPrincipal.Identity as ClaimsIdentity;
if (identity == null)
return;
var existingClaim = identity.FindFirst("Organization");
if (existingClaim != null)
identity.RemoveClaim(existingClaim);
// add new claim
identity.AddClaim(new Claim("Organization", value.ToString()));
}
}
}
}
I have the same problem as well
Do you have a solution for this ?
Dear Ismac,
I have another Issue here. It would be great if you can give me a solution.
I am able to set the Claim in my Session as below. It is perfectly setting the value (REF 1.png)
When it is fetching for the first time, it is perfectly taking the correct value but it is doing for the second time.
Let me summarise the things i have done and let me know how to solve this
public class DineConnectDataModule : AbpModule
{
public override void PreInitialize()
{
//web.config (or app.config for non-web projects) file should containt a connection string named "Default".
Configuration.DefaultNameOrConnectionString = "Default";
Configuration.UnitOfWork.RegisterFilter("ConnectFilter", true);
}
public override void Initialize()
{
//AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
// (context as DineConnectDbContext).AuditEntries.AddRange(audit.Entries);
Configuration.Auditing.IsEnabled = true;
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DineConnectDbContext, Migrations.Configuration>());
}
}
namespace DinePlan.DineConnect.Filter
{
public interface IOrganization
{
int Oid { get; set; }
}
}
public override void Initialize()
{
base.Initialize();
this.SetFilterScopedParameterValue("ConnectFilter", "oid", ConnectSession.OrgId);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Filter("ConnectFilter", (IOrganization entity, int oid) => entity.Oid == oid, 0);
}
private async Task SignInAsync(User user, ClaimsIdentity identity = null, bool rememberMe = false)
{
if (identity == null)
{
identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
}
identity.AddClaim(new Claim("Organization", "1"));
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity);
}
public class SessionAppService : DineConnectAppServiceBase, ISessionAppService
{
private readonly ConnectSession _mySession;
public SessionAppService(ConnectSession mySession)
{
_mySession = mySession;
}
}
public class ConnectSession : ITransientDependency
{
public int OrgId
{
get
{
var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
var tenantIdClaim = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == "Organization");
if (!string.IsNullOrEmpty(tenantIdClaim?.Value))
{
return Convert.ToInt32(tenantIdClaim.Value);
}
return 0;
}
set
{
var currentPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
var identity = currentPrincipal.Identity as ClaimsIdentity;
if (identity == null)
return;
var existingClaim = identity.FindFirst("Organization");
if (existingClaim != null)
identity.RemoveClaim(existingClaim);
// add new claim
identity.AddClaim(new Claim("Organization", value.ToString()));
}
}
}
Ref IMAGE 1
Ref IMAGE 2
exec sp_executesql N'INSERT [dbo].[MenuItems]([Name], [BarCode], [AliasCode], [AliasName], [ItemDescription], [ForceQuantity], [ForceChangePrice], [CategoryId], [TenantId], [ProductType], [Tag], [RefLocation], [TransactionTypeId], [HsnCode], [Oid], [IsDeleted], [DeleterUserId], [DeletionTime], [LastModificationTime], [LastModifierUserId], [CreationTime], [CreatorUserId])
VALUES (@0, NULL, @1, NULL, NULL, @2, @3, @4, @5, @6, NULL, @7, NULL, NULL, @8, @9, NULL, NULL, NULL, NULL, @10, @11)
SELECT [Id]
FROM [dbo].[MenuItems]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()',N'@0 nvarchar(100),@1 nvarchar(50),@2 bit,@3 bit,@4 int,@5 int,@6 int,@7 int,@8 int,@9 bit,@10 datetime2(7),@11 bigint',@0=N'TTTT',@1=N'43234',@2=0,@3=0,@4=2,@5=1,@6=1,@7=0,@8=0,@9=0,@10='2017-08-23 09:43:33.3571316',@11=2
go
exec sp_executesql N'SELECT
[Extent1].[Id] AS [Id],
[Extent1].[BarCode] AS [BarCode],
[Extent1].[AliasCode] AS [AliasCode],
[Extent1].[AliasName] AS [AliasName],
[Extent1].[CategoryId] AS [CategoryId],
[Extent2].[Name] AS [Name],
[Extent1].[CreationTime] AS [CreationTime],
[Extent1].[CreatorUserId] AS [CreatorUserId],
[Extent1].[Name] AS [Name1],
[Extent1].[ForceChangePrice] AS [ForceChangePrice],
[Extent1].[ForceQuantity] AS [ForceQuantity],
[Extent1].[ItemDescription] AS [ItemDescription]
FROM (SELECT [Var_17].[Id] AS [Id], [Var_17].[Name] AS [Name], [Var_17].[BarCode] AS [BarCode], [Var_17].[AliasCode] AS [AliasCode], [Var_17].[AliasName] AS [AliasName], [Var_17].[ItemDescription] AS [ItemDescription], [Var_17].[ForceQuantity] AS [ForceQuantity], [Var_17].[ForceChangePrice] AS [ForceChangePrice], [Var_17].[CategoryId] AS [CategoryId], [Var_17].[CreationTime] AS [CreationTime], [Var_17].[CreatorUserId] AS [CreatorUserId]
FROM [dbo].[MenuItems] AS [Var_17]
WHERE (([Var_17].[Oid] = @DynamicFilterParam_7) OR (@DynamicFilterParam_8 IS NOT NULL)) AND (([Var_17].[TenantId] = @DynamicFilterParam_5) OR (@DynamicFilterParam_6 IS NOT NULL)) AND (([Var_17].[IsDeleted] = @DynamicFilterParam_1) OR (@DynamicFilterParam_2 IS NOT NULL)) ) AS [Extent1]
LEFT OUTER JOIN (SELECT [Var_18].[Id] AS [Id], [Var_18].[Name] AS [Name]
FROM [dbo].[Categories] AS [Var_18]
WHERE (([Var_18].[TenantId] = @DynamicFilterParam_5) OR (@DynamicFilterParam_6 IS NOT NULL)) AND (([Var_18].[IsDeleted] = @DynamicFilterParam_1) OR (@DynamicFilterParam_2 IS NOT NULL)) ) AS [Extent2] ON [Extent1].[CategoryId] = [Extent2].[Id]
ORDER BY [Extent1].[Name] ASC
OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY ',N'@DynamicFilterParam_7 int,@DynamicFilterParam_8 bit,@DynamicFilterParam_5 int,@DynamicFilterParam_6 bit,@DynamicFilterParam_1 bit,@DynamicFilterParam_2 bit',@DynamicFilterParam_7=1,@DynamicFilterParam_8=NULL,@DynamicFilterParam_5=1,@DynamicFilterParam_6=NULL,@DynamicFilterParam_1=0,@DynamicFilterParam_2=NULL
go
Thanks
One last question
Do I need to look in any other place?
Got it How to set the Value into the Property of Custom Session?
Please let me know. It will be really helpful.
I need to set the Property based on Combo Value Change. I can call the Session Service to do that.
You have not answered for the first question
How to get the session value in the context class Initialize method ?
The DATA FILTER values are setting in the Intialize method as per your document
Please let me know
How to set for all the Services at one place ?
Hello Team
Is there any way to set all the ApplicationServices as AbpAuthorizr without mention in each method ?
Thanks in advance
Thanks for the Answer
How to get the Custom Session Property and set it in Context Initialize Method.
For Example, I have to set it in
public override void Initialize()
{
base.Initialize();
this.SetFilterScopedParameterValue("ConnectFilter", "oid", 1);
}
and my session is
public class ConnectSession : ITransientDependency
{
public int Location { get; set; }
}
[AbpAuthorize]
public class SessionAppService : DineConnectAppServiceBase, ISessionAppService
{
private readonly ConnectSession _mySession;
public SessionAppService(ConnectSession mySession)
{
_mySession = mySession;
}
[DisableAuditing]
public async Task<GetCurrentLoginInformationsOutput> GetCurrentLoginInformations()
{
var output = new GetCurrentLoginInformationsOutput
{
User = (await GetCurrentUserAsync()).MapTo<UserLoginInfoDto>()
};
bool mutlipleUomAllowed = false;
if (PermissionChecker.IsGranted("Pages.Tenant.House.Transaction.MultipleUOMAllowed"))
{
mutlipleUomAllowed = true;
}
output.User.MultipleUomEntryAllowed = mutlipleUomAllowed;
output.User.LocationRefId = GetCurrentUserLocationAsync();
output.Location = (await GetLocationInfo(output.User.LocationRefId)).MapTo<LocationListDto>();
if (AbpSession.TenantId.HasValue)
{
output.Tenant = (await GetCurrentTenantAsync()).MapTo<TenantLoginInfoDto>();
}
return output;
}
}