分部

@if (Request.IsAuthenticated)
{
<text>
你好,@Html.ActionLink(User.Identity.Name, "Manage", "Admin", routeValues: null, htmlAttributes: new { @class = "username", title = "管理" })!
@using (Html.BeginForm("LoginOut", "Account", FormMethod.Post, new { id = "LoginOutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('LoginOutForm').submit()">注销</a>
}
</text>
}
else
{ <text>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<p>
@Html.ActionLink("登录", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })
@Html.ActionLink("注册", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })
</p>
}
</text>
}

登录控制器代码

MsgContext db = new MsgContext();

 

[HttpGet]
public ActionResult Login()
{
return View();
}

 

 

[HttpPost]
public ActionResult Login(Login admin)
{
var user = db.Admins.Where(a => a.UserName == admin.UserName
&& a.Password == admin.Password).FirstOrDefault();
if (user != null)
{
//创建一个新的票据,将用户的名字记入ticket的userdata
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(20),
false, user.AdminRole.ToString());
//将票据加密
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
//将加密后的票据保存为cookie
System.Web.HttpCookie authCookie = new System.Web.HttpCookie
(FormsAuthentication.FormsCookieName, encryptedTicket);
//使用加入了userdata的新cookie
System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
return RedirectToAction("Index", "Admin");
}
return View();
}

转载于:https://www.cnblogs.com/shenbinlei/p/5100338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值