1
私はASP.NETで始まり、認証に問題があります。ASP.NET MVCでの認証
これはコードです:
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
と呼ばれるクッキーが返されますが、プロファイルの観点では、h1
は表示されません。
正しくはないですか?
可能な複製(http://stackoverflow.com/questions/32564984/using-formsauthentication – GSerg
[User.Identity.IsAuthenticatedの重複している可能性のある部分は、Cookieを設定して検証した後にfalseを返します](http://stackoverflow.com/q/14508495/11683) – GSerg