로그인 화면이있는 웹 사이트를 만들고자합니다. 그러나 우리에게는 문제가 있습니다. 우리의 도메인은 localhost/Login/User입니다. 그러나 사용자가 localhost/Home/Index를 입력하면 로그인하지 않고도 메인 사이트에 접속할 수 있습니다. 그래서 우리는 색인 컨트롤러에 [Authorize]라고 썼습니다. 그러나 나는 내가 사용해야하는 것을 발견 할 수 없었다. 우리 프로젝트에서 AuthorizeAttribute를 사용해야합니까?컨트롤러의 MVC 인증
1
A
답변
1
가 [AllowAnonymous] 속성을 추가하는 방법 내 인 LoginController/인증 기능에 인덱스에 액세스 할 수 있습니다
#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("/");
}
}
인덱스 페이지
[Authorize]
public ActionResult Index()
{
Session["ip"] = Request.UserHostAddress;
if (IsDbExists())
{
_contactList = new List<Contact>();
UpdateOperations();
return View(_contactList);
}
Response.Redirect("/Loading/LoadingScreen");
return null;
}
. [AllowAnonymous] 속성을 가진 AuthController라는 다른 컨트롤러를 추가하여 사용자가 실제로 로그인하지 않고 로그인 할 수있게했습니다.
일반적으로 모든 컨트롤러를 기본적으로 필터링하고 [AllowAnonymous] 특성을 누구든지 액세스 할 수있는 것.
나는이를 처리하기 위해 이것을 사용한다.
using System.Web.Mvc;
namespace Test
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
}
}
AuthController의 [AllowAnonymous] 속성 예입니다. 당신이 도움이 될
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;
}
}
}
참고 : 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
관련 문제
- 1. Spring MVC 컨트롤러의 인증 메소드
- 2. 인증 MVC
- 3. PHP MVC 컨트롤러의 POST?
- 4. ASP.NET MVC - 컨트롤러의 작업
- 5. MVC 컨트롤러의 JsonResult
- 6. MVC API 컨트롤러의 목적
- 7. 스프링 MVC 컨트롤러의 생성자
- 8. 컨트롤러의 MVC 값 입력
- 9. MVC 컨트롤러의 격자 구성
- 10. ASP.Net MVC 컨트롤러의 세계화
- 11. MVC 컨트롤러의 전역 데이터
- 12. cakephp로 컨트롤러의 사용자 인증 확인하기
- 13. 스프링 MVC 커스텀 인증
- 14. ASP.NET MVC 컨트롤러의 수명은 어떻게됩니까?
- 15. generics 대 mvc 컨트롤러의 상속
- 16. MVC -보기에서 컨트롤러의 메서드를 호출해야합니다.
- 17. ASP의 ViewModel과 컨트롤러의 차이점 MVC
- 18. ASP.NET MVC 컨트롤러의 대상 _top
- 19. asp.net mvc 컨트롤러의 수명이란 무엇입니까?
- 20. mvc 컨트롤러의 내 UserSession이 Null입니까?
- 21. Spring MVC 컨트롤러의 트랜잭션 롤백
- 22. Castle Windsor MVC 컨트롤러의 IOC
- 23. 면도기가있는 MVC 3 컨트롤러의 리디렉션
- 24. MVC 컨트롤러의 작업 인터셉트 방법
- 25. MVC - 나는 두 컨트롤러의 행동이
- 26. 컨트롤러의 일부 오류 (asp.net mvc)
- 27. MVC 컨트롤러의 이벤트를 테스트하는 방법
- 28. 컨트롤러의 ASP.NET MVC 모델 목록
- 29. MVC : 뷰와 컨트롤러의 카디널리티 관계
- 30. PHP 용 MVC 프레임 워크에서 컨트롤러 인증