繁体   English   中英

ASP.NET MVC中的身份验证

[英]Authentication in ASP.NET MVC

我从ASP.NET开始,我的身份验证问题。

这是代码:

web.config中

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/users/login" timeout="3000"  />
  </authentication>
  <compilation debug="true" targetFramework="4.5.2" />
  <httpRuntime targetFramework="4.5.2" />
</system.web>

登录

public ActionResult login()
{
    if (Request.HttpMethod == "POST")
    {
        if (Request.Form["email"] == null || Request.Form["password"] == null)
            ViewData["error"] = "form_error";
        else
        {
            User user = this.dal.authUser(
            Request.Form["email"],
            Request.Form["password"]);
            if (user == null)
                ViewData["error"] = "auth_error";
            else
            {
                FormsAuthentication.SetAuthCookie(user.id.ToString(), false);
                return Redirect("/profiles/" + user.id);
            }
        }
    }
    return (View());
}

轮廓控制器

[HttpGet]
public ActionResult get(int id)
{
    ViewData["auth"] = false;
    if (HttpContext.User.Identity.IsAuthenticated)
        ViewData["auth"] = true;
    Response.Write(HttpContext.User.Identity.Name);
    Profile profile = this.dal.getProfile(id);
    if (profile == null)
        return View("~/Views/error404.cshtml");
    ViewData["profile"] = profile;
    return View();
}

和查看获取个人资料

@using Plume.Areas.Users.Models;

@{
    Layout = "~/Views/Layout/layout.cshtml";
    Variables.pageTitle = "Profil";
    Profile profile = (Profile)ViewData["profile"];
    bool auth = (bool)ViewData["auth"];
}

@{ 
    if (auth)
    {
        <h1>Auth</h1>
    }
}

<p>
    ceci est le profil de : 
    @profile.username
    <br />
    dont l'email est :
    @profile.user.email
</p>

因此,当我记录时,会返回一个名为.ASPXAUTH的cookie,但在配置文件的视图中,不会显示h1

什么是不正确的?

请在Web.Config文件中删除或评论以下行。

Web.Config:

 <modules>
  <!--<remove name="FormsAuthenticationModule" />-->
</modules>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM