ASP.NET Core Identity by default uses cookies to store
claims. If your authorization is very granular then you will end up using many
claims. With all these claims stored in cookie, the cookie size gets bigger and
you can exceed the cookie limit.
We can achieve the authorization granularity without
exceeding the cookie limit and impacting the way ASP.NET Identity authorizes
users by storing the claims in session, Redis or any other memory storage.
Below are the steps I implemented to store the claims in
session.
First let’s store the claims in session (assuming that the
ASP.NET Core Session is already configured). During authentication, Identity
system uses SignInManager for creating principal object of the logged in user.
While creating the principal object, Identity populates the claims. We can
override this SignInManager and store the claims as shown below:
public class ApplicationSignInManager : SignInManager<ApplicationUser>
{
private IHttpContextAccessor contextAccessor;
public
ApplicationSignInManager(UserManager<ApplicationUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<ApplicationUser>> logger)
: base(userManager,
contextAccessor, claimsFactory, optionsAccessor, logger)
{
this.contextAccessor =
contextAccessor;
}
public override async Task<ClaimsPrincipal>
CreateUserPrincipalAsync(ApplicationUser user)
{
var principal = await base.CreateUserPrincipalAsync(user);
ClaimsIdentity identity = (ClaimsIdentity)principal.Identity;
//
storing claims in session and removing them. These claims will be added by
Transformer
List<ClaimModel> sessionClaims = new List<ClaimModel>();
List<Claim> identityClaims =
identity.Claims.ToList();
foreach (var claim in identityClaims)
{
sessionClaims.Add(new ClaimModel() { ClaimType =
claim.Type, ClaimValue = claim.Value });
identity.RemoveClaim(claim);
}
this.contextAccessor.HttpContext.Session.SetString("IdentityClaims", JsonConvert.SerializeObject(sessionClaims));
return principal;
}
}
The Identity system should be configured to use our ApplicationSignInManager
instead of the default one. For this we need to define the dependency injection
in the Startup under ConfigureServices
services.AddScoped<SignInManager<ApplicationUser>, ApplicationSignInManager>();
As you see with above code claims are removed from the
principal identity and stored in the session. These claims should to be added
back to the principal identity for every request. This done through the claims transformer
as shown below:
public class ClaimsTransformer : IClaimsTransformer
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext context)
{
ClaimsPrincipal principal =
context.Principal;
ClaimsIdentity identity = (ClaimsIdentity)principal.Identity;
string claimString = NTContext.HttpContext.Session.GetString("IdentityClaims");
if (claimString != null)
{
List<ClaimModel> sessionClaims = JsonConvert.DeserializeObject<List<ClaimModel>>(claimString);
identity.AddClaims(sessionClaims.Select(sc => new Claim(sc.ClaimType,
sc.ClaimValue)));
}
return Task.FromResult(principal);
}
}
This ClaimsTransformer class is
configured in the Startup under Configure section as shown below:
app.UseClaimsTransformation(new ClaimsTransformationOptions()
{
Transformer = new ClaimsTransformer(),
});
app.UseIdentity();
With the above setup now the claims are stored in session when
the user logs in. At every request from the user these claims will be copied
from session to the identity object. Now we have overridden the default
approach of using cookies with session J
Oh, they are really hard to distinguish from each other! Thanks for the help
ReplyDeleteThe saving all applications is optimal in each case. However, this also has a limit. The users will be able to leave their requests for review.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteFascinating post. I Have Been considering about this issue, so thankful for posting. Totally cool post.It 's very generally very Useful post.Thanks
ReplyDeletedata science course
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeleteartificial intelligence course
https://istanbulolala.biz/
ReplyDeleteOMUML
AF92D
ReplyDeleteMuş Evden Eve Nakliyat
Yalova Parça Eşya Taşıma
Tunceli Evden Eve Nakliyat
Silivri Duşa Kabin Tamiri
Rize Parça Eşya Taşıma
Bibox Güvenilir mi
Kocaeli Parça Eşya Taşıma
Denizli Evden Eve Nakliyat
Paribu Güvenilir mi
333C9
ReplyDeleteBitcoin Nasıl Alınır
Adana Parça Eşya Taşıma
Kayseri Şehir İçi Nakliyat
Diyarbakır Parça Eşya Taşıma
Manisa Şehirler Arası Nakliyat
Çorlu Lojistik
Siirt Evden Eve Nakliyat
Sincan Boya Ustası
Karaman Şehir İçi Nakliyat
7FBFD
ReplyDeleteGiresun Lojistik
Eskişehir Şehir İçi Nakliyat
Paribu Güvenilir mi
Pursaklar Boya Ustası
Iğdır Şehirler Arası Nakliyat
Bartın Şehirler Arası Nakliyat
Kütahya Parça Eşya Taşıma
Gümüşhane Parça Eşya Taşıma
Çorum Şehir İçi Nakliyat
D07DF
ReplyDeleteTekirdağ Fayans Ustası
Çerkezköy Korkuluk
Düzce Evden Eve Nakliyat
Malatya Evden Eve Nakliyat
Pancakeswap Güvenilir mi
Tekirdağ Evden Eve Nakliyat
Çankırı Evden Eve Nakliyat
Yenimahalle Fayans Ustası
Eryaman Parke Ustası
30610
ReplyDeleteBinance Nasıl Oynanır
Bitcoin Hesap Açma
Kripto Para Nasıl Çıkarılır
Mexc Borsası Kimin
Bitcoin Nasıl Kazanılır
Bitcoin Üretme
Binance Yaş Sınırı
Binance Madenciliği Nedir
Kripto Para Nasıl Alınır
69EA8
ReplyDeleteBitcoin Kazanma Siteleri
resimlimag.net
Coin Madenciliği Nedir
Bitcoin Madenciliği Nedir
magnet
Paribu Borsası Güvenilir mi
Binance Madenciliği Nedir
Bitcoin Nasıl Oynanır
Bitcoin Nasıl Üretilir
2A9A5
ReplyDeletebingx
bitcoin seans saatleri
kizlarla canli sohbet
October 2024 Calendar
kripto ne demek
bybit
August 2024 Calendar
kripto para nasıl alınır
kucoin
D89A0
ReplyDeletematadorbet
----
----
----
----
----
----
----
----
BC652
ReplyDeleteQnt Coin Yorum
Aave Coin Yorum
Lsk Coin Yorum
Crv Coin Yorum
Arb Coin Yorum
Bitcoin Son Dakika Haberleri
Dash Coin Yorum
Ada Coin Yorum
Qtum Coin Yorum