Solved

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

Posted on 2013-10-29
5
653 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

19 Experts available now in Live!

Get 1:1 Help Now