Problem with a login control event
I want to have a c开发者_如何学JAVAheck as soon as the user logs in whether he was banned or not.. But I have the following problem:
protected void Login1_LoggedIn(object sender, EventArgs e)
{
MembershipUser CurrentUser = Membership.GetUser();
Guid i = (Guid)CurrentUser.ProviderUserKey; //CurrentUser is null
if (UsefulStaticMethods.CheckIfUserISbanned(i))
{
Server.Transfer("~/Banned.aspx");
}
}
How do I get the current user as soon as he makes the login?
Do it here:
protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
or
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
Use Login.Authenticate Event to find out is the user is banned or not.
As Mantrork says your best bet is to override
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
At this point the Membership.GetUser() is going to be blank however, the user has not yet finished authentication. Instead you need to pull the username from your login control;
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = Membership.ValidateUser(Login1.UserName, Login1.Password) &&
if (Authenticated)
{
MembershipUser user = Membership.GetUser(Login1.UserName);
Guid i = (Guid)user.ProviderUserKey; //CurrentUser is null
if (UsefulStaticMethods.CheckIfUserISbanned(i))
{
Server.Transfer("~/Banned.aspx");
}
}
e.Authenticated = Authenticated;
}
(Code is from the top of my head, so not ran through VS)
Login.Authenticate return true if user is authenticated in web application
If your Login1 is a standard asp:Login control you can use:
MembershipUser CurrentUser = Membership.GetUser(LoginUser.UserName);
精彩评论