【问题标题】:Authentication in ASP.NET MVCASP.NET MVC 中的身份验证
【发布时间】:2016-10-20 21:10:52
【问题描述】:

我从 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

什么不正确?

【问题讨论】:

标签: c# asp.net asp.net-mvc authentication forms-authentication


【解决方案1】:

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

Web.Config:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-05-03
    相关资源
    最近更新 更多