Asp.net 将登录逻辑置于局部视图中
作为一名新手,我几乎在MVC中开发了一款考勤应用程序linq to sql。现在卡在一个地方,登录部分,当特定用户的会话开始时,右上角会出现注销按钮,这没关系,但我想做的是,我得到一个当前存储在会话中的id(员工id),然后在右上角显示,如Asp.net 将登录逻辑置于局部视图中,asp.net,.net,asp.net-mvc,c#-4.0,linq-to-sql,Asp.net,.net,Asp.net Mvc,C# 4.0,Linq To Sql,作为一名新手,我几乎在MVC中开发了一款考勤应用程序linq to sql。现在卡在一个地方,登录部分,当特定用户的会话开始时,右上角会出现注销按钮,这没关系,但我想做的是,我得到一个当前存储在会话中的id(员工id),然后在右上角显示,如 EMPLOYEE 002 has Logged in, [LOG OUT] 注销实际上是一个正在呈现的局部视图,我已经将我的逻辑用于会话检查 @if(Session["Employee"] != null) { @*<text>Wel
EMPLOYEE 002 has Logged in,
[LOG OUT]
注销实际上是一个正在呈现的局部视图,我已经将我的逻辑用于会话检查
@if(Session["Employee"] != null) {
@*<text>Welcome <strong>@User.Identity.Name</strong>!*@
<text> [ @Html.ActionLink("Log Out", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log In", "LogOn", "Account") ]
}
答复
@if(会话[“员工”!=null){
int-empId=0;
int.TryParse((字符串)会话[“Employee”],out empId);
@empId已登录
[@Html.ActionLink(“注销”、“注销”、“帐户”)]
}
否则{
@:[@Html.ActionLink(“登录”、“登录”、“帐户”)]
}
使用@User.Identity.Name时会发生什么?又是什么问题?您不知道如何获取员工id或如何显示它?为什么要在会话
对象上进行测试?您需要调用Request.IsAuthenticated
。如果您在会话[“员工”]
中存储了某种信息,甚至可能是员工ID,那么只需将@User.Identity.Name
替换为@Session[“员工”]
。请不要认为这是不好的做法……尝试如下:“如果(请求。IsAudivice){Hello,<代码> @用户.No.<代码> },然后使用<代码> > IF(Envest.Is认证){Base`EnviID登录}{…} <代码>,我很高兴您能运行它,但是请考虑更改< <代码>如果语句> <代码> @ IF。(Request.IsAuthenticated)。
public ActionResult LogOff()
{
Session.Abandon();
return RedirectToAction("Index", "Home");
}
@if(Session["Employee"] != null) {
int empId = 0;
int.TryParse((string)Session["Employee"], out empId);
<text> <strong>@empId </strong> has logged In <br />
[ @Html.ActionLink("Log Out", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log In", "LogOn", "Account") ]
}