C# 检查用户是否经过身份验证始终会导致true
我试图检查一个用户何时经过身份验证,并且我总是得到该用户是否经过身份验证。这是我的密码:C# 检查用户是否经过身份验证始终会导致true,c#,asp.net,C#,Asp.net,我试图检查一个用户何时经过身份验证,并且我总是得到该用户是否经过身份验证。这是我的密码: if( User.Identity.IsAuthenticated ) { addProfiledata(); } 即使我登录和注销,此条件始终为真 如何更正此问题?请尝试以下方法: if(HttpContext.Current.Request.IsAuthenticated) { //put code for Authenticated user } 或者交替地 if(Use
if( User.Identity.IsAuthenticated )
{
addProfiledata();
}
即使我登录和注销,此条件始终为真
如何更正此问题?请尝试以下方法:
if(HttpContext.Current.Request.IsAuthenticated) {
//put code for Authenticated user
}
或者交替地
if(User.IsInRole("rolename")) {
//put code for Authenticated user
}
希望这会有帮助 如果您已经定义了用户角色,那么应该对其进行处理,您使用的是LoginControl,我猜您正在使用其他登录控件(如loginview)处理sing out sing in,希望您将用户角色分配给它。如果是这种情况,您可以使用LoginUser\u LoggedIn事件
您是否使用FormsAuthentication.Signout()从应用程序中注销。您在哪里设置他不再经过身份验证?我使用了LoginControl,我做了一个测试,它会自动使用我将尝试它您使用的是哪种身份验证,窗体还是windows?我已经尝试了HttpContext.Current.Request.IsAuthenticated它现在仍然有效内部的方法仍然得到执行 // suppose if you are willing to redirect a user according to his/her role protected void LoginUser_LoggedIn(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) { // if user in role and authenticated if (Roles.IsUserInRole(LoginUser.UserName, "Developers")) { // add session and redirect to particular page Session.Add("developer", "Developers"); Response.Redirect("../Developers/devAccess.aspx");
} } }