How to get System Login and logoff time in ASP.net MVC4 -


am new asp.net mvc4 want program can record system login time , system logoff time along ip address.

thanks in advance....

to record client ip address use

httpcontext.request.userhostaddress //in controller  or   httpcontext.current.request.userhostaddress// in classes 

to record login , logout time.

create custom action filter class , record datetime given below.


[customfilter(currentaction="login")] public actionresult login() {   ... }  [customfilter(currentaction="logout")] public actionresult logout() {   ... } 

public class customfilter : actionfilterattribute {      string currentaction {get; set;}      void iactionfilter.onactionexecuting(actionexecutingcontext filtercontext)         {         // todo: add action filter's tasks here        mydbentities storedb = new mydbentities ();        if(currentaction.equals("login"))       {            logindetail log = new logindetail ()            {               logintime= datetime.now,               ip = filtercontext.httpcontext.request.userhostaddress            };            storedb.logindetails.add(log);       }       else if(currentaction.equals("logout"))       {           logindetail log = new logindetail ()           {             logouttime= datetime.now,             ip = filtercontext.httpcontext.request.userhostaddress           };           storedb.logindetails.add(log);       }        storedb.savechanges();        this.onactionexecuting(filtercontext);     } } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -