Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

ASP.net MVC4 "Simple Membership" -- user log file ?

How can I write the below logfile ?

Example
 1. user logs into localhost as johnDoe
 2. system looks up user in webpages_Membership database table
 3. attached LOG records get written....
s010.jpg
0
finance_teacher
Asked:
finance_teacher
  • 3
  • 2
1 Solution
 
finance_teacherAuthor Commented:
Basically I want to create a new record in the webpages_Membership_LogFile database table instead of doing the below webpages_Membership "Modified" function like
http://stackoverflow.com/questions/14321696/simple-membershiplast-login-date-of-user
talks about.

How is this possible ?
----------------------------------------------------------------------------------------------
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid
                && !WebSecurity.IsAccountLockedOut(model.UserName, 3, 86400) //after 3 attempts, account locked for 24hrs
                && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {

                using (UsersContext db = new UsersContext())
                {
                    UserProfile userProfile = db.UserProfiles.SingleOrDefault(u => u.UserName == model.UserName);
                    userProfile.LastLogin = DateTime.Now;
                    db.Entry(userProfile).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
0
 
finance_teacherAuthor Commented:
Any suggestions ?
0
 
deanvanrooyenCommented:
hi,


I dont have web security installed, but check if there is a proc that is invoked by the web security login method (run sql query analyser), you could probably add some extra sql in that proc to do what you require. I wouldn't add it to the profile store as this is more auditing. Also look at a logger like log4net, this give you more flexibility and more re-usability.
0
 
finance_teacherAuthor Commented:
Added below to AccountController.cs, works
-----------------------------------------------------------------------
using MWR.Data;

namespace MWR.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class AccountController : Controller
    {

        private MVC_testEntities db = new MVC_testEntities();
        dataDB dataDB = new dataDB();

. . . . . etc . . . . .

        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid
                && !WebSecurity.IsAccountLockedOut(model.UserName, 3, 86400) //after 3 attempts, account locked for 24hrs
                && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                //*
                webpages_Membership_LogFile wpml = new webpages_Membership_LogFile();
                wpml.UserName = model.UserName;
                wpml.LoginDateTime = DateTime.Now;
                wpml.Status = "SUCCESS";
                db.webpages_Membership_LogFile.Add(wpml);
                db.SaveChanges();
                 //*/

                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form

            //*
            webpages_Membership_LogFile wpm2 = new webpages_Membership_LogFile();
            wpm2.UserName = model.UserName;
            wpm2.LoginDateTime = DateTime.Now;
            wpm2.Status = "FAIL";
            db.webpages_Membership_LogFile.Add(wpm2);
            db.SaveChanges();
            //*/

            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
0
 
deanvanrooyenCommented:
great glad you got it working...
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now