1

Je commence par ASP.NET et j'ai un problème avec l'authentification.Authentification dans ASP.NET MVC

Voici le code:

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> 

connexion

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()); 
} 

contrôleur de profil

[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(); 
} 

et pour obtenir un Voir le profil

@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> 

Alors, quand je me connecte, un cookie appelé .ASPXAUTH est retourné, mais dans la vue de profil, la h1 n'est pas affiché.

Qu'est-ce qui n'est pas correct?

+0

double possible de [Utilisation FormsAuthentication pour se connecter et utiliser HttpContext.Current.User.Identity] (http://stackoverflow.com/questions/32564984/using-formsauthentication -pour-connecter-et-utiliser-httpcontext-current-user-identity) – GSerg

+0

Copie possible de [User.Identity.IsAuthenticated renvoie false après le réglage du cookie et de la validation] (http://stackoverflow.com/q/14508495/11683) – GSerg

Répondre

0

Veuillez supprimer ou commenter la ligne ci-dessous dans le fichier Web.Config.

web.config:

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