如何添加用户 ID TI AUTHENTICATED A cookie
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
namespace project.Controllers
{
public class LoginController : Controller
{
// GET: /Login/
private DataContext db = new DataContext();
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult index(Loginmodel model)
{
if (ModelState.IsValid)
{
String username = model.Username;
User Login = db.Users.Where(m => m.Name == username).First();
String pass = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(model.Password)));
if (Login != null && pass == Login.Password)
{
FormsAuthentication.SetAuthCookie(model.Username, false);
return RedirectToAction("index", "Project");
}
ModelState.AddModelError("", "Invalid username or password");
}
return View();
}
}
}