2016-10-20 12 views
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は表示されません。

正しくはないですか?

+0

可能な複製(http://stackoverflow.com/questions/32564984/using-formsauthentication – GSerg

+0

[User.Identity.IsAuthenticatedの重複している可能性のある部分は、Cookieを設定して検証した後にfalseを返します](http://stackoverflow.com/q/14508495/11683) – GSerg

答えて

0

Web.Configファイルの下の行を削除またはコメントしてください。

のWeb.Config:[ログイン用をFormsAuthenticationを使用してHttpContext.Current.User.Identityを使用する]の

<modules> 
    <!--<remove name="FormsAuthenticationModule" />--> 
</modules> 
関連する問題