2013-07-25 94 views
0

我正在使用mvc4和c#构建网站。我有一个管理员控制器。我想验证该控制器。在ASP.Net中授权管理控制器MVC 4

我有一个基于角色的SQL数据库表用户(包含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文件或其他文件中是否有任何更改?

回答

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