0

我正在使用 mvc4 和 c# 构建一个网站。我有一个管理员控制器。我想验证那个控制器。

我有一个基于角色的 SQL db 表用户(包含 User_NA、User_pwd 和 User_role)我如何登录到管理索引页面。

[Authorize]   
public ActionResult Index(string id="")
{
     ViewBag.Admin = id;
     return View();
}

我有一个登录和注销操作。

[HttpGet]
public ViewResult Login()
{
    return View();
}
[HttpPost]
public RedirectResult Login(FormCollection form)
{
    string uid = Request.Form["userid"];
    string pwd = Request.Form["password"];
    ...............................
    else return Redirect("~/Admin/Login");
}

public RedirectResult Logout()
{
    System.Web.Security.FormsAuthentication.SignOut();
    return Redirect("~/Admin");
}

如何将身份验证代码写入登录操作?Web.Config 文件或任何其他文件是否有任何变化?

4

1 回答 1

1

你可以使用 WebSecuriy.Login 方法

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, 
          model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
于 2013-07-25T05:28:13.307 回答