2016-08-31 1 views
1

Nous essayons de faire un site Web qui a un écran de connexion. Mais nous avons un problème. Notre domaine est localhost/Login/Utilisateur. Mais si l'utilisateur entre localhost/Home/Index, il/elle peut accéder à notre site principal sans connexion. Nous avons donc écrit [Authorize] sur notre Index Controller. Mais je ne pouvais pas savoir ce que je devais utiliser. Dois-je utiliser AuthorizeAttribute dans notre projet?Authentification MVC dans le contrôleur

#Login Page 
public class LoginController : Controller 
{ 
    //GET: Login 
    [IntranetAction] 
    public ActionResult Users() 
    { 
     return View(); 
    } 

    public ActionResult Authentication(UserLoginInfo loginInfo) 
    { 
     bool isAuthenticated = new LdapServiceManager().isAuthenticated(loginInfo); 


     if (isAuthenticated) 
     { 
      //AUTHORIZED 
      Session["userName"] = loginInfo.username; 
      return Redirect("/Home/Index"); 
     } 
     //WORNG PASSWORD, BACK TO LOGIN PAGE 
     TempData["message"] = "Yanlış kullanıcı adı ya da şifre"; 
     return Redirect("/"); 
    } 
} 

Index Page

[Authorize] 
public ActionResult Index() 
{ 
    Session["ip"] = Request.UserHostAddress; 
    if (IsDbExists()) 
    { 
     _contactList = new List<Contact>(); 
     UpdateOperations(); 
     return View(_contactList); 
    } 

    Response.Redirect("/Loading/LoadingScreen"); 
    return null; 
} 

Comment puis-je accéder à l'index dans ma LoginController/fonction d'authentification

Répondre

1

Ajouter un attribut [AllowAnonymous]. J'ajouterais un autre contrôleur appelé AuthController qui aurait un attribut [AllowAnonymous] afin que les utilisateurs puissent se connecter sans être connecté.

Je filtre généralement tous les contrôleurs par défaut et ajoute l'attribut [AllowAnonymous] à ceux qui seraient accessibles par n'importe qui.

Je l'utilise pour régler cela.

using System.Web.Mvc; 

namespace Test 
{ 
    public class FilterConfig 
    { 
     public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
     { 
      filters.Add(new HandleErrorAttribute()); 
      filters.Add(new AuthorizeAttribute()); 
     } 
    } 
} 

Un exemple de l'attribut [AllowAnonymous] dans AuthController.

using System.Security.Claims; 
using System.Web; 
using System.Web.Mvc; 
using BusinessLogic.Services; 
using Common.Models; 
using Microsoft.AspNet.Identity; 
using Microsoft.Owin.Security; 

namespace Test.Controllers 
{ 
    [AllowAnonymous] 
    public class AuthController : Controller 
    { 
     private readonly IUsersService _usersService; 

     public AuthController(IUsersService usersService) 
     { 
      _usersService = usersService; 
     } 

     [HttpGet] 
     public ActionResult LogIn() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult LogIn(LoginModel loginModel) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(); 
      } 

      var isValid = _usersService.AuthenticateUser(loginModel); 
      if (isValid) 
      { 
       var identity = new ClaimsIdentity(new[] 
       { 
        new Claim(ClaimTypes.NameIdentifier, loginModel.Username), 
        new Claim(ClaimTypes.Name, loginModel.Username), 
       }, DefaultAuthenticationTypes.ApplicationCookie); 

       Request.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity); 

       return Redirect(GetRedirectUrl(loginModel.ReturnUrl)); 
      } 

      ModelState.AddModelError("", "Invalid credentials"); 
      return View(); 
     } 

     public ActionResult LogOut() 
     { 
      var ctx = Request.GetOwinContext(); 
      var authManager = ctx.Authentication; 

      authManager.SignOut("ApplicationCookie"); 
      return RedirectToAction("index", "home"); 
     } 

     private string GetRedirectUrl(string returnUrl) 
     { 
      if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl)) 
      { 
       return Url.Action("index", "home"); 
      } 
      return returnUrl; 
     } 
    } 



} 

Références qui pourraient vous aider: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

https://softwareengineering.stackexchange.com/questions/284380/is-formsauthentication-obsolete

Role-based access control (RBAC) vs. Claims-based access control (CBAC) in ASP.NET MVC

https://www.owasp.org/index.php/.NET_Security_Cheat_Sheet