ASP.NET MVC 学习 --- 第六课(根据用户名登录网页) log on log off
2012-05-11 10:42
363 查看
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
try
{
//ManageService 中定义了验证用户名的方法 VerifyUserLogon, 在之前的第四课中写了如何去验证用户名密码
ManageService _manageService = new ManageService();
if (ModelState.IsValid)
{
//判断输入的用户名密码是否正确
bool _isUserLogon = _manageService.VerifyUserLogOn(model.UserName, model.Password);
string role = "Admin";
if (_isUserLogon)
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, //版本
model.UserName, //用户名
DateTime.Now, //时间
DateTime.Now.AddDays(2), //过期时间
false, //是否一直有效
role);
//将新的Ticket转变为Cookie值,并添加到Cookies集合中
string encTicket = FormsAuthentication.Encrypt(authTicket);
this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
this.Response.Cookies.Add(new HttpCookie("role", role));
}
}
return RedirectToAction("Index", "Home");
// If we got this far, something failed, redisplay form
}
catch(Exception ex)
{
throw new Exception("Log on failed:" + ex.Message);
}
}
{
try
{
//ManageService 中定义了验证用户名的方法 VerifyUserLogon, 在之前的第四课中写了如何去验证用户名密码
ManageService _manageService = new ManageService();
if (ModelState.IsValid)
{
//判断输入的用户名密码是否正确
bool _isUserLogon = _manageService.VerifyUserLogOn(model.UserName, model.Password);
string role = "Admin";
if (_isUserLogon)
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, //版本
model.UserName, //用户名
DateTime.Now, //时间
DateTime.Now.AddDays(2), //过期时间
false, //是否一直有效
role);
//将新的Ticket转变为Cookie值,并添加到Cookies集合中
string encTicket = FormsAuthentication.Encrypt(authTicket);
this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
this.Response.Cookies.Add(new HttpCookie("role", role));
}
}
return RedirectToAction("Index", "Home");
// If we got this far, something failed, redisplay form
}
catch(Exception ex)
{
throw new Exception("Log on failed:" + ex.Message);
}
}
相关文章推荐
- Asp.net MVC 3实例学习之ExtShop(六)——登录对话框
- Asp.net MVC 3实例学习之ExtShop(六)——登录对话框
- ASP.NET MVC学习笔记 -- NerdDinner实战2 -- Nod32造成IE无法打开localhost网页
- Asp.net MVC 3实例学习之ExtShop(六)——登录对话框
- Asp.net MVC 3实例学习之ExtShop(六)――登录对话框
- ASP.NET MVC数据验证 登录 注册用 学习留用
- Asp.net MVC 3实例学习之ExtShop(六)——登录对话框
- 学习: ASP.NET MVC开发入门-UI实现根据查询条件返回Model层的查询结果
- ASP.NET MVC 单点登录(Single Signe On)
- 《ASP.NET MVC 4 实战》学习笔记 1:第一个MVC用程序(上)
- ASP.NET MVC学习之高级Controller和Action
- 从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇
- iOS学习系列 - 在iOS客户端实现google oauth2登录以及在asp.net服务端上form认证
- ASP.NET MVC 3.0学习系列文章--序
- ASP.NET MVC 3.0学习系列文章
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.11.ASP.NET MVC3的细节:概览MVC项目
- ASP.NET MVC 5 学习教程:通过控制器访问模型的数据
- (asp.net MVC学习)System.Web.Mvc.UrlHelper的学习与使用
- asp.net mvc OnActionExecuting 方法重写后的执行顺序
- ASP.NET MVC 5 学习教程:创建连接字符串