Solved

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

Posted on 2013-10-29
5
662 Views
Last Modified: 2013-11-22
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
Comment
Question by:finance_teacher
  • 3
  • 2
5 Comments
 

Author Comment

by:finance_teacher
ID: 39608579
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
 

Author Comment

by:finance_teacher
ID: 39616677
Any suggestions ?
0
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 500 total points
ID: 39620105
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
 

Author Comment

by:finance_teacher
ID: 39631660
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
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39631957
great glad you got it working...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now