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