3

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 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer i want to know is there any simple method provide by Identity to implement this kind of tracking. i just want to track user login time and logout time
@Wella, I don't think and know of any method provided by the Identity Framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.