分部
2016-01-04 21:35
239 查看
@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();
}
{
<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();
}
相关文章推荐
- hdu 3033 I love sneakers! 分组背包
- JavaScript:理解constructor
- 机器学习(四):分类算法之朴素贝叶斯
- UE4 Hello World 创建第一个UE4工程
- UE4 Hello World 创建第一个UE4工程
- javascript--出错与调试
- Socket的综合应用总结
- nginx 或tengine 访问日志分割处理
- Spring中 <tx:advice/> 标签
- IOS 委托代理(delegate)实现页面传值
- 数据库第四章
- iOS动态启动界面(swift实现)
- Ubuntu安装g++
- iOS开发之基础控件(Swift版)
- 2016新年第一记
- CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)
- C++的const类成员函数
- 在线SVN托管服务器
- groovy : poi 导出 Excel
- 浅谈js的几种模式(二)