[英]React - How to hide register/login in navbar once a user has logged in?
[英]How to hide Login fields from the logged user
我正在使用ADO.net开发ASP MVC。 我试图在用户登录后隐藏登录字段。
实际上,在从主页进行身份验证之后,用户将被重定向到他的配置文件页面。 问题在于,当用户返回首页时,他总是会找到登录字段。
根据调试器,Session [“ currentUser”]始终保持为空,然后呈现脚本。
在开发工具中我也没有发现错误。
这是我尝试过的:
@if (Session["currentUser"] != null)
{
<script type='text/javascript'>
$(document).ready(function(){
$("#login").hide();
});
</script>
}
<div class="login" id="login">
@*@RenderPage("~/Views/Home/Login.cshtml")*@
<link href="~/Content/toastr.css" rel="stylesheet" />
<div class="main-w3l">
<div class="w3layouts-main" style="background-image:url('/template/web/images/bg3.jpg'); margin-top:50px;">
<h2>Login Now</h2>
@using (Html.BeginForm("Login", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<input value="E-MAIL" name="Email" type="email" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-Mail';}" />
<input value="PASSWORD" name="Password" type="password" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'password';}" />
<span><input type="checkbox" />Remember Me</span>
<h6><a href="#">Forgot Password?</a></h6>
<div class="clear"></div>
<input type="submit" value="login" name="login">
}
<p>Don't Have an Account ?<a href="#" onclick="@("window.location.href='" + @Url.Action("Create", "Client") + "'") ;">Register Now</a></p>
</div>
</div>
更新:控制器:
public ActionResult Login()
{
return View();
}
database_Access_layer.db dblayer = new database_Access_layer.db();
[HttpPost]
public ActionResult Login(FormCollection fc, string LastName, string Email)
{
int res = dblayer.Admin_Login(fc["Email"], fc["Password"]);
if (res == 1)
{
Session["currentUser"] = Email;
string z = Email;
connection();
con.Open();
SqlCommand command = new SqlCommand("select Email from Client", con);
List<string> result = new List<string>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
result.Add(reader.GetString(0));
con.Close();
}
foreach (string x in result)
{
if (x == z)
{
SqlCommand command2 = new SqlCommand($"select LastName from Client WHERE Email= '{x}' ", con);
con.Open();
string y = command2.ExecuteScalar().ToString();
con.Close();
Session["currentUser"] = y;
}
}
return RedirectToAction("Profil", "Client");
Session.RemoveAll();
}
else {
TempData["msg"] = " Email or Password is wrong !";
return RedirectToAction("Index", "Home");
}
}
有内置的系统可以帮助您进行登录。 出于多种原因,不应使用会话变量。
要设置登录名,您可以在控制器内部使用以下功能:
FormsAuthentication.SetAuthCookie(UserName, remember);
同样,您可以使用FormsAuthentication.SignOut()
登出。
现在,在剃须刀中或任何控制器中,您都可以检查User.Identity.IsAuthenticated
以验证用户是否已登录。
为了使此功能起作用,您还需要确保在<system.web>
下的web.config下具有以下内容:
<authentication mode="Forms">
</authentication>
如果想利用此登录功能,还可以将登录表单添加到与首页不同的单独的cshtml页面中。 可以说此页面位于Home/Login
位置。 现在,您可以在web.config中修改以上代码,如下所示
<authentication mode="Forms">
<forms loginUrl="~/Home/Login" timeout="2880" />
</authentication>
这将分配您的默认登录页面。 现在,如果仅在登录时需要访问任何URL,如果用户未登录,它将自动重定向到该位置。要指定操作需要身份验证,请在操作之前使用[Authorize]
,或在需要整个控制器时使用整个控制器整个控制器授权。 您的操作将如下所示:
[Authorize]
public ActionResult Index(){
return View()
}
最终,您的示例HomeController
可以如下所示:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Login()
{
return View(); // for the login form
}
[HttpPost]
public void Login(string UserName, .... <other fields>)
{
// validate your login first here
FormsAuthentication.SetAuthCookie(UserName, true);
}
}
将您知道用户是否登录的令牌/相关信息存储在会话/缓存中,然后在编写用户登录功能的任何地方都可以编写任何服务,或者仅使用jquery / javascript进行访问,然后显示或隐藏表格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.