Saturday, March 24, 2012

AJAX and Membership API

Hi All,

I have grown fed up with the restrictions placed on using the Login Controls provided by VWD, so I've decided to build my own controls and then call on the membership API class to insert records. I mainly did this to allow my application to use AJAX on the login features, but I have run into a problem.

Here is the code for my problem:

 
"ScriptManager1" runat="server" /> "UpdatePanel1" runat="server"> "Button1" runat="server" Text="Button" OnClick="Button1_Click" /> "LoginName1" runat="server" /> "msg" runat="server" Text="Label"> "LoginStatus1" runat="server" />

It is a simple page that has a login button, and when you click the button it runs this code:
protected void Button1_Click(object sender, EventArgs e) {if (Membership.ValidateUser("Brainify","BrainifyPassword")) { FormsAuthentication.SetAuthCookie("Brainify",true); msg.Text ="Login success!!!. Please check your user name and password and try again."; }else { msg.Text ="failed"; } } }

The code will try to validate a user (hardcoded) that I've put into the database. And if successful it will cause the msg label to change text. That works fine. But I would also like it change the the loginname control. This is all done through AJAX.

The problem is when I click on the button once, it validates and it changes the msg label to what I want. How the login name control never appears. I actually have to click on the button ONE more time to get the login name control to display the username! I have NO Idea what is going on...it appears AJAX is working, but I don't know why I have to click the button twice for everything to appear.

Any help would be greatly appreciated.Hi,

This is because the LoginName is determined by the current request's?user identity. When you click the button to login, the request?still doesn't contain a valid UserName.
The value can only be retrieved in the requests after this. So that an additional click is required.

You can use?Sys.Services.AuthenticationService to authenticate on the client before a postback happens.
Like this:

<"Button1" runat="server" Text="Button"?OnClientClick="javascript function that invokes Sys.Services.AuthenticationService" OnClick="Button1_Click" /
Hope this helps.

No comments:

Post a Comment