Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-10-29
5
Medium Priority
?
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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