using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Test.Models;
namespace Test.Controllers
{
public class HomeController : Controller
{
WS_WebAppContext entity = new WS_WebAppContext();
//登录页面
public ActionResult Login()
{
return View();
}
//检查登录信息
[HttpPost]
public ActionResult Login(User u)
{
var logined = entity.Users.SingleOrDefault(m => m.Account == u.Account);
if (!string.IsNullOrWhiteSpace(u.Password) && logined != null && logined.Password == u.Password)
{
String role = "User";
if (logined.IsAdmin)
{
role = "Admin";
}
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
logined.Id.ToString(),
DateTime.Now,
DateTime.Now.AddMinutes(120),
false,
role
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(authCookie);
if (logined.IsAdmin)
{
return RedirectToAction("Index", "Admin");//跳转到管理员的主页
}
else
{
return RedirectToAction("Index", "User");//跳转到用户的主页
}
}
else
{
return Content("<script>alert('用户名或密码错误!');local.href='/Home/Index'</script>");
}
}
}
}