Login control and ViewState on successful logon

Posted by: Jotekes Blog, on 28 Jun 2007 | View original | Bookmarked: 0 time(s)

On Forums someone had issues with Login control, as he had custom authentication scheme in use and in Authenticate event handler, set a value to ViewState. And he had hard time figuring why the value wasn't there on next request when user was logged in (he checked it on the login page, e.g. didn't have a redirect page, right after authentication occurred). Something like:

'Aspx

<asp:Button ID="Button1" runat="server" OnClick="g" Text="Show view state"/><br />
           viewstate: <asp:Label ID="v1" runat="server" /><br />
<div>
asp:Login ID="Login1" runat="server" OnAuthenticate="F"/>

'Code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Test if postback occurs
        Response.Write(IsPostBack)
    End Sub

'Try to read from ViewState
    Sub G(ByVal sender As Object, ByVal e As System.EventArgs)
        v1.Text = ViewState("a")
    End Sub

'Put something to ViewState
    Sub F(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
        ViewState("a") = "test"
        e.Authenticated = True
    End Sub

Reason is that on successful login, Login control does a redirect to the specified target page, even if it's the same page the controls resides on. Postback does occur (Login control handles bubbled Command event coming from the logon Button) but during it in it's own internal AttemptLogin method, the control does a Response.Redirect after it has delegated a call to FormsAuthentication.SetAuthCookie. You can confirm this by checking what IsPostBack returns on Page_Load. It's false when you click  ther Log-In button and True if you click Show ViewState button.

This is fine in practise for all scenarios, but might surprise you if you think you need ViewState during the logon process and expect Login control work in postbacking sense, so I thought to mention it here.

P.S when authentication fails, it does redirect only if you have set LoginFailure to RedirectToLoginPage

Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1114 | Hits: 110

Similar Posts

  • SEO Tip - Beware of the Login pages - add them to Robots Exclusion more
  • DotNetNuke Case Study: DNNTreeView for Large DNN Applications more
  • CodeDigest.Com Article,Codes,FAQs - April,2009 more
  • Easy way to create a web-based AJAX SFTP Client application more
  • How to use Ninject with ASP.NET MVC more
  • ASP.NET MVC Plan 9 Sample more
  • DomainDataSource Server Control: LINQ + Code Generation more
  • Handling Formats Based On Url Extension more
  • MVC Controllers and Forms Authentication more
  • How to call controllers in external assemblies in an ASP.NET MVC application more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD