Where can I set last login time when using ASP.NET and Owin?
up vote
0
down vote
favorite
I have implemented authentication using ASP.NET identity. The login is actually performed in this method of the 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.");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
At that point, I don't have access to the User table where a field called "lastLoginAt" exists. I need to update that field to the date and time when the user logged in.
I have also a custom user store which has a method defined this way:
public Task UpdateAsync(T user)
but that method will be called if the User entity has to be updated.
Where do you suggest to add the code to update the last login date and time?
c# asp.net-mvc asp.net-identity owin
add a comment |
up vote
0
down vote
favorite
I have implemented authentication using ASP.NET identity. The login is actually performed in this method of the 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.");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
At that point, I don't have access to the User table where a field called "lastLoginAt" exists. I need to update that field to the date and time when the user logged in.
I have also a custom user store which has a method defined this way:
public Task UpdateAsync(T user)
but that method will be called if the User entity has to be updated.
Where do you suggest to add the code to update the last login date and time?
c# asp.net-mvc asp.net-identity owin
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have implemented authentication using ASP.NET identity. The login is actually performed in this method of the 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.");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
At that point, I don't have access to the User table where a field called "lastLoginAt" exists. I need to update that field to the date and time when the user logged in.
I have also a custom user store which has a method defined this way:
public Task UpdateAsync(T user)
but that method will be called if the User entity has to be updated.
Where do you suggest to add the code to update the last login date and time?
c# asp.net-mvc asp.net-identity owin
I have implemented authentication using ASP.NET identity. The login is actually performed in this method of the 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.");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
At that point, I don't have access to the User table where a field called "lastLoginAt" exists. I need to update that field to the date and time when the user logged in.
I have also a custom user store which has a method defined this way:
public Task UpdateAsync(T user)
but that method will be called if the User entity has to be updated.
Where do you suggest to add the code to update the last login date and time?
c# asp.net-mvc asp.net-identity owin
c# asp.net-mvc asp.net-identity owin
asked Nov 21 at 23:56
jstuardo
96452860
96452860
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
As for me I used it this way.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userStore = new UserStore<ApplicationUser>(new ExtractorDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var user = await manager.FindAsync(context.UserName, context.Password);
if (user != null)
{
string roleName = manager.GetRoles(user.Id).FirstOrDefault();
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserId", user.Id));
identity.AddClaim(new Claim("Username", user.UserName));
identity.AddClaim(new Claim("Email", user.Email));
identity.AddClaim(new Claim("FirstName", user.FirstName));
identity.AddClaim(new Claim("LastName", user.LastName));
identity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
identity.AddClaim(new Claim("Role", roleName));
context.Validated(identity);
}
else
{
return;
}
}
You can modify your ClaimsIdentity oAuthIdentity same way of my ClaimsIdentity.
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
As for me I used it this way.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userStore = new UserStore<ApplicationUser>(new ExtractorDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var user = await manager.FindAsync(context.UserName, context.Password);
if (user != null)
{
string roleName = manager.GetRoles(user.Id).FirstOrDefault();
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserId", user.Id));
identity.AddClaim(new Claim("Username", user.UserName));
identity.AddClaim(new Claim("Email", user.Email));
identity.AddClaim(new Claim("FirstName", user.FirstName));
identity.AddClaim(new Claim("LastName", user.LastName));
identity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
identity.AddClaim(new Claim("Role", roleName));
context.Validated(identity);
}
else
{
return;
}
}
You can modify your ClaimsIdentity oAuthIdentity same way of my ClaimsIdentity.
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
add a comment |
up vote
0
down vote
As for me I used it this way.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userStore = new UserStore<ApplicationUser>(new ExtractorDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var user = await manager.FindAsync(context.UserName, context.Password);
if (user != null)
{
string roleName = manager.GetRoles(user.Id).FirstOrDefault();
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserId", user.Id));
identity.AddClaim(new Claim("Username", user.UserName));
identity.AddClaim(new Claim("Email", user.Email));
identity.AddClaim(new Claim("FirstName", user.FirstName));
identity.AddClaim(new Claim("LastName", user.LastName));
identity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
identity.AddClaim(new Claim("Role", roleName));
context.Validated(identity);
}
else
{
return;
}
}
You can modify your ClaimsIdentity oAuthIdentity same way of my ClaimsIdentity.
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
add a comment |
up vote
0
down vote
up vote
0
down vote
As for me I used it this way.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userStore = new UserStore<ApplicationUser>(new ExtractorDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var user = await manager.FindAsync(context.UserName, context.Password);
if (user != null)
{
string roleName = manager.GetRoles(user.Id).FirstOrDefault();
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserId", user.Id));
identity.AddClaim(new Claim("Username", user.UserName));
identity.AddClaim(new Claim("Email", user.Email));
identity.AddClaim(new Claim("FirstName", user.FirstName));
identity.AddClaim(new Claim("LastName", user.LastName));
identity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
identity.AddClaim(new Claim("Role", roleName));
context.Validated(identity);
}
else
{
return;
}
}
You can modify your ClaimsIdentity oAuthIdentity same way of my ClaimsIdentity.
As for me I used it this way.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userStore = new UserStore<ApplicationUser>(new ExtractorDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var user = await manager.FindAsync(context.UserName, context.Password);
if (user != null)
{
string roleName = manager.GetRoles(user.Id).FirstOrDefault();
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserId", user.Id));
identity.AddClaim(new Claim("Username", user.UserName));
identity.AddClaim(new Claim("Email", user.Email));
identity.AddClaim(new Claim("FirstName", user.FirstName));
identity.AddClaim(new Claim("LastName", user.LastName));
identity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
identity.AddClaim(new Claim("Role", roleName));
context.Validated(identity);
}
else
{
return;
}
}
You can modify your ClaimsIdentity oAuthIdentity same way of my ClaimsIdentity.
answered Nov 22 at 1:31
klitz
285
285
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
add a comment |
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Thanks... I have done it, but I need to know the other part now. Where does the actual user saving occur? i am using a custom user store. I have placed a breakpoint at UpdateAsync method of the user store that receives the IdentityUser as parameter. My custom IdentityUser contains the LastLoggedOn property but i could not find where i need to implement the actual record persistance to database.
– jstuardo
Nov 22 at 12:16
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
Have you tried to use that function the same line with your ClaimsIdentity?
– klitz
Nov 26 at 0:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422092%2fwhere-can-i-set-last-login-time-when-using-asp-net-and-owin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown