Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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