I need to track user logins into MS Sql server data table with asp.net Identity on MVC5 Web Application what is the best way to implement this .
1 Answer
If you implemented the authentication parts of your application like described in this tutorial, the place where you can trigger the tracking is easy to find, right after the call to SignInManager.PasswordSignInAsync. I'd suggest to track all attempts, not only successful logins. There are several ways of doing this and it all depends of your existing architecture, which frameworks you already using etc. If you don't have anything yet, you can follow this tutorial to get a Entity Framework Data Context up and running in MVC5, but you would create a Entity which has all the useful fields for tracking user logins (e.g. Timestamp, username and result of login attempt) e.g.:
public enum LoginResult
{
Success,
LockedOut,
RequiresVerification,
Failure
}
public class UserLogin
{
public DateTime DateTime { get; set; }
public LoginResult LoginResult { get; set; }
public string Username { get; set; }
}
and you simply add to your Data Context:
var appContext = new AppContext();
appContext.UserLogins.Add(new UserLogin { DateTime = DateTime.UtcNow, LoginResult = result /* Depending of result */, Username = model.EMail });
appContext.SaveChanges();
This is very simplified but you get the idea. Of course this will cost you some further research and development.