Home » forms authentication » Forms Authentication – Manual Authentication

Forms Authentication – Manual Authentication

F03I0043

I’ve had several occasions in the past where I’ve needed to do my own authentication or I’ve needed to add some additional methods to the authentication process.

As easy as Microsoft has made the authentication process, you might think that in order to  manually authenticate you’d need to write all of your authentication code manually.  But nothing could be farther from the truth.

In fact, most of the time all you need to do is trap an event handler in the existing login control.

A couple of years ago, I was asked to create a login page that used a web service to authenticate the user.  I also needed to add another form field to the login screen, so it became obvious that to do this I’d need to turn the login control into a templated control.

Once this was done it was a simple matter to trap the click event of the login button, authenticate against the web service, and then set the authentication cookie for ASP.NET.

Since I can’t show you how to authenticate against the service–your implementation will almost certainly be different–we will skip that section.  But to set the cookie, all we need to do is to revert to the ASP.NET 1.1 way of setting up the login.

if (Request.QueryString["ReturnUrl"] != null)
{
    FormsAuthentication.RedirectFromLoginPage
        (m_tbUsername.Text, persistentCookie); } else { FormsAuthentication.SetAuthCookie
        (m_tbUsername.Text, persistentCookie); Response.Redirect("~/"); }

The first line checks to see if there was a Return URL specified.  If there was we can use the RedirectFromLoginPage API.  Otherwise, we need to set the Authentication Cookie manually and redirect on our  own.

The persistentCookie parameter is true if we want the user to always be logged in.  Otherwise, the login is for the session.

 

Other post in forms authentication

Related Post

  • Determine The Role of a User in ASP.NETDetermine The Role of a User in ASP.NET There are several controls that allow you to display content based on the role a user is in, including: - LoginView - LoginStatus And the web.config file allows us to control which […]
  • Setting Up Your Forms Based Authentication DatabaseSetting Up Your Forms Based Authentication Database I was recently asked if I would cover some topics related to Forms Based Authentication.  The person who requested this information has some specific issues that he wants covered […]
  • Manually Adding Event Handlers in VB.NETManually Adding Event Handlers in VB.NET Typically when we write our code, the event handlers get wired up for us using the handles clause.  So we never have to worry about wiring up our event handlers manually. But what […]
  • GridView and Updating A Row ManuallyGridView and Updating A Row Manually A couple of days ago I mentioned a project that I’ve been working on that is a bit out of the ordinary as far as GridViews go.  One of the issues I’ve had is that the edit template […]
  • ASP.NET GridView Edit All Rows At OnceASP.NET GridView Edit All Rows At Once I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go... Here's the problem.  You want to be able to edit […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

  • Salman Butt

    Why cant you just show us the code for authenticating against the service as well? It doesnt matter if our implementations are different – we can at least the get the feel of what exactly the entry is about. Please post some sample authentication code as well as how to tap into login control events?