Last post Dec 07, 2018 06:59 PM by jstuardo
Dec 04, 2018 11:44 PM|jstuardo|LINK
Hello, to save the date and time when user logs in, I have do this in GrantResourceOwnerCredentials of my custom OAuthAuthorizationServerProvider:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
context.SetError("invalid_grant", "El nombre de usuario o la contraseña no son correctos.");
context.SetError("invalid_grant", "El usuario está bloqueado. Contáctese con el administrador.");
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
oAuthIdentity.AddClaim(new Claim("LastLoggedOn", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")));
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
As you see, I am adding a claim, but by doing this, no actual user update occurs.
how can I do it?
Dec 06, 2018 03:13 AM|Nan Yu|LINK
Hi jstuardo ,
In your scenario , you just add claim to user's claim ,so that the client/resource could access the claim like :
((ClaimsIdentity)User.Identity).Claims.FirstOrDefault(x => x.Type == "LastLoggedOn").Value;
That is not related to update/insert that value to your database . If you want to save the LastLoggedOn properties in your database , you should add logic to update related database table using EF :
Dec 06, 2018 05:14 PM|jstuardo|LINK
Hello Nan Yu,
that StackOverflow question is not related to my problem. It just explains how to add a new field that is populated when registering.
That is not my problem since I can perfectly add custom fields and populate and in fact, my SQL Server table is not the default AspNetUsers table but a completely custom table called "Usuarios".
My problem is related to update certain field (in this case LastLoggedIn field) just after login. None of my custom methods to update a user is called when user is logged in, so that is the question.
When GrantResourceOwnerCredentials method of my custom AuthProvider returns after user authenticated, it returns the token to the page, but I don't know if there is an instance I can use to update the LasttLoggedIn date.
Since my login is performed by mean of an Ajax call, I am thinking to make a new ajax request to the MVC controller just to update that field, but I think this may be tricky.
Dec 06, 2018 08:03 PM|mgebhard|LINK
Simply set the LastLoggedIn property, I assume the property exists on the user type and the DB, then save. The syntax looks similar to...
user.LastLoggedIn = DateTime.Now
var result = await userManager.UpdateAsync(user);
Dec 06, 2018 09:22 PM|jstuardo|LINK
Yes... finally I did this exactly as you wrote:
user.LastLoggedOn = DateTime.Now;
Dec 07, 2018 02:01 AM|Nan Yu|LINK
None of my custom methods to update a user is called when user is logged in, so that is the question.
As i said , none of your codes are related to the database operation , you should update the property in database using EF , i assume you haven't added the column in user table , so i provide the SO thread link to help you make the migration , After that
, you can use update statement to update related column .
Dec 07, 2018 06:59 PM|jstuardo|LINK
I have finally used mgebhard suggestion. It was as simple as adding the update line in GrantResourceOwnerCredentials method of authorization provider.