0

mvc4 と c# を使用して Web サイトを構築しています。管理コントローラーがあります。そのコントローラーを認証したい。

管理者インデックス ページにログインする方法に基づいて、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 に答える