相关疑难解决方法(0)

ASP.NET MVC 3使用身份验证

如何使用FormsAuthentication保存内容?我不想通过URL存储UserId.

例如,现在我有这个代码:

//UserController class:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
  if (repository.ValidateUser(model.Login, model.Password))
  {
    FormsAuthentication.SetAuthCookie(model.Login, model.RememberMe);
    if (Url.IsLocalUrl(returnUrl))
    {
      return Redirect(returnUrl);
    }
    else
    {
      return RedirectToAction("Project", "Index");
    }
  }
  else
  {
     ModelState.AddModelError("", "Incorrect name or password.");
  }
}

return View(model);
}
Run Code Online (Sandbox Code Playgroud)

ProjectController 类:

public ViewResult Index()
{
    return View(repository.GetUserProjects(
        this.ControllerContext.HttpContext.User.Identity.Name));
}
Run Code Online (Sandbox Code Playgroud)

ProjectRepository:

ProjectsContext context = new ProjectsContext();
UsersContext uCnt = new UsersContext();

public IEnumerable<Project> GetUserProjects(String username)
{
    if (String.IsNullOrEmpty(username))
        throw new ArgumentNullException("username", "Login is …
Run Code Online (Sandbox Code Playgroud)

authentication asp.net-mvc-3

22
推荐指数
2
解决办法
3万
查看次数

标签 统计

asp.net-mvc-3 ×1

authentication ×1