您的位置:首页 > 编程语言 > ASP

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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: