Hi everyone, I'm a novice with asp core 3 and I'm developing a system from scratch, the system has custom security for issues that I won't go into detail about, what happens is that I want to use Claims but I'm stuck and I can't find anything in the network on how to use claims, I have this: To put them in context this is the custom security database schema
To assign the users I want to use Claims and this is what I have first in the startup.cs in the configure services section
#region Implementacion de Claims
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.Configure<CookiePolicyOptions>(opt =>
{
opt.MinimumSameSitePolicy = SameSiteMode.None;
});
#endregion
and in the always configure section in the startup.cs I have this:
app.UseAuthentication();
app.UseAuthorization();
app.UseCookiePolicy();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
in the controller I have this:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(UsuarioModel Entidad)
{
var eData = await _iUser.ObtenerPorEmail(Entidad.email);
//Si no encuentra el correo no esta registrado el user
if (eData == null)
{
TempData["Titulo"] = "Error";
TempData["Mensaje"] = "El usuario con que intenta conectar no existe, pongase en contacto con el admnistrador del sistema";
return RedirectToAction("Mensaje", "Home");
}
//Si llega hasta aquí y es primera vez permite generar el password
if (eData.firsttime)
{
return RedirectToAction("SavePassword", eData);
}
else
{
//Si no es primera vez verifica si es correcto el pass y se conecta
if (eData.ComparePass(Entidad.passmain, eData.salt, eData.password))
{
//Aqui codigo de claims
return RedirectToAction("Index", "Home");
}
TempData["Titulo"] = "Error";
TempData["Mensaje"] = "El password esta equivocado";
return RedirectToAction("Mensaje", "Home");
}
}
Well I've reached the point where I have to implement the claims but I don't know how: 1) Create the claims with the data at that moment? 2) How do I reuse the claim to validate if I connected? 3) How can I get the claims to reuse it and disable it in the options menu? 4) How do I destroy them when I close the session?
I know there are a lot of questions :(, but if someone has a place where I can see a clear example and can read more about this I would appreciate it or if you can put an example it would be great, thanks in advance to the community.
(update) works perfectly in 3.1 or higher I've tried it with 5.x too Ok. I am going to auto-respond because I have found the solution by reading and by trial and error. I want to share it with the community in case someone needs information. This works in ASP CORE 3.X and of course I know that there may be better ways, so always if you know of a better one, I invite you to post it to learn more
First configure startup.cs section ConfigureServices
It is important to say that this is a minimum security configuration, that is why I use the option opt.MinimumSameSitePolicy = SameSiteMode.None; but you can read more and make your claims more secure
Very well. let's always continue in startup.cs Configure section
Add these options if they are not.
That is so that the claims work for us
we will answer the questions
First thing I need these usign in the controller
Then we can create the claims
as you can see very simple a list of claims and then add there are "ClaimTypes" these are predefined in case you use identity you can use it without identity as I have done but you can also define your own "userid", "Name", "shit" what you want :D "eData" is a class of mine that brings the log data.
After creating the necessary Claims we must create the identity using the list of claims created above, so that we can use them later this is very easy
Well, the only thing left to do is log in, in case I have told you that if I don't disconnect, these claims only last 8 hours.
How do I reuse the claim to validate if I connected? That is to say, how do I know that it is connected and the claims exist, very easy, we only look for a claim that I created, for example "ClaimTypes.Name" there, save the name of the user
if data is null if (data != null) then there are no claims if it is not null because the user is connected.
the first thing is to use the following using
then we can do the following
and these variables we can use as we want in the views or razor pages
and the final question.
How do I destroy them when I log out? this in controller
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
In this way I destroy them even though the time I defined for the existence of the claims has not ended.
well and that's it I hope it helps someone greetings