ASP.NET Account lockout notify

19. May 2011 21:05

 

Something that people normally overlook when building a system is logging so here is a little snippet of code that will detect when an account has become locked out making it possible to notify either support staff or the end user that there current account has been locked out.

 

This following works by taking a reading of the user account before and after the login event. So we read the status of the account on the page postback. Then we test for it during the login error. We can then log this or send an email to the current user.

 

 

public partial class Login : AppPage
{
    private bool AccountWasLocked = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
        {
            MembershipUser tmp = Membership.GetUser(Login2.UserName);
            if (tmp != null)
                AccountWasLocked = tmp.IsLockedOut;
        }
    }

    protected void Login2_LoginError(object sender, EventArgs e)
    {
        MembershipUser tmp = Membership.GetUser(Login2.UserName);

        if (tmp != null && AccountWasLocked == false && tmp.IsLockedOut == true)
        {
            string Msg = string.Format("User '{0}' Has Locked Out their Account from IP Address '{1}'", tmp.UserName, Request.UserHostAddress);
            DBLog.Log(DBConn, tmp, "Login.LoginError.AccountLocked", Msg);
            //Notify Admin / End User
        }

        DBLog.Log(DBConn, "Login.Failure", string.Format("Login failed for user '{0}' from {1}", Login2.UserName, Request.UserHostAddress));
    }

    protected void Login2_LoggedIn(object sender, EventArgs e)
    {
        DBLog.Log(DBConn, "Login", string.Format("User '{0}' has logged in from {1}", Login2.UserName, Request.UserHostAddress));
    }
}

 

 

In the above code you will also need to include the namespace System.Web.Security to be able to access the MemberShip functions in asp.net.

 

The class DBLog that I am using has two static functions which you will also need to implement depending on how you wish to store your log information.

 

public class DBLog
{
    public static void Log(DBConn Conn, MembershipUser User, string LogType, string Message);
    public static void Log(DBConn Conn, string LogType, string Message);
}

 

I will probably be making some posts in the near future about logging in asp.net

E-mail Kick it! DZone it! del.icio.us Permalink


ASP.NET - Blocking By IP Address

14. April 2011 22:55

 

Recently I seem to be running into a little bit of a spamming problem with backlink's being submitted to the blog. It looks like the way .net blog engine does post is really easy for the spammer to be able to post lots and lots of comments with bots (I guess the spammers are getting good at this now). This sounds quite bad but everything is being caught by the spam filters so its really not so bad. So this is a bit of a guide to attempt to protect a website from such action coming from abusive computers around the internet. More...

E-mail Kick it! DZone it! del.icio.us Permalink


C# - Saving Settings using serialization

10. March 2011 18:50

Almost every windows program that is written for windows at some stage needs to save some settings ... Somewhere. Some method are better that others depending on what needs to be be saved. Here is a method todo that by serializing an entire class and saving it in the user app data folder on their computer.

 

It does not require much to enable this sort of functionality. Though it can be far superior to the existing set that exists by default in visual studio as it allows you to save an active data state for example and then when the program restarts it's possible to continue from where you left off.

More...

E-mail Kick it! DZone it! del.icio.us Permalink


C# - Logoff

7. March 2011 19:49

Here is a quick set of functions that will allow you to reboot / shutdown / logoff windows from c#

 

I was flicking though some things on another asp.net / c# and stumbled across the following post. It is aimed at providing a solution in VB.NET to reboot a computer. However I think its a pretty poor attempt at solving the problem so I have been digging though my code base and found the following solution.

More...

E-mail Kick it! DZone it! del.icio.us Permalink


ASP.NET - Dynamic Controls

2. March 2011 19:18

This is a quick guide to show you away of creating as many dynamic controls as is required on a web page. For some reason a lot of developers typically seem to struggle when dealing with dynamic controls. They really are not that difficult to understand so long as you follow a few basic rules and understand a few myths about dynamic controls. More...

E-mail Kick it! DZone it! del.icio.us Permalink