如何检查用户是否使用asp.net mvc登录用户控件
通常在视图页面上我使用它
<% if (User.Identity.IsAuthenticated) {%>
//Do something
<% } %>
Run Code Online (Sandbox Code Playgroud)
但我无法在用户控件上完成此操作
我想在mvc4中创建login和logOut函数.在login func中,如果登录cookie存在且不为空,则用户处于signIn模式,否则重定向到登录页面.在logOut func中,所有cookie和会话都清除并重定向到login func,但是在login func中存在登录cookie!
登录:
public ActionResult Login()
{
if (Request.Cookies["login"] != null)
{
string login = Request.Cookies["login"].Value.ToString();
if (login != string.Empty)
{
//Get from service
Service srv = new Service();
UserItem userItem = srv.getUserItem(login);
srv.Close();
Session.Timeout = 30;
Session["login "] = login;
Session["userId"] = userItem.No;
Session["firstName"] = userItem.FirstName;
Session["lastName"] = userItem.LastName;
string loginName = userItem.LoginName;
FormsAuthentication.SetAuthCookie(loginName, false);
return Redirect(“Index”);
}
else
{
Return redirect("http://mySite/SignIn.aspx");
}
}
else
{
Return redirect("http://mySite/SignIn.aspx");
}
}
Run Code Online (Sandbox Code Playgroud)
登出:
public ActionResult LogOut()
{
string …Run Code Online (Sandbox Code Playgroud)