문제

I have a .Net Login Control with a event handler for onloggedin.

onloggedin="Login2_LoggedIn"

However User.Identity is always null.

 protected void Login2_LoggedIn(object sender, EventArgs e)
{
    // Is User is Admin
    if (Roles.IsUserInRole(User.Identity.Name, "admin"))

Is it supposed to be available at this point? Or should I get the user name from the object sender or EventArgs e?

도움이 되었습니까?

해결책

Page.User is not available until the page posts back after authenticating, so it is null on the page hosting the login control. However, you can still call the method by using the UserName property of the login control. This is safe to do in the LoggedIn event handler because the user has been authenticated at this point.

if (Roles.IsUserInRole(Login2.UserName, "admin"))
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top