1

asp.net-core에서 액션 본문의 위조 방지 토큰을 확인하기 위해 AntiForgery.Validate();과 유사한 명령이 있습니까? DOTNET 4.5.1asp.net-core의 AntiForgery.Validate()와 같은 명령

코드 예 :

public ActionResult Index() 
{ 
    if (!User.Identity.IsAuthenticated) 
    { 
     System.Web.Helpers.AntiForgery.Validate(); 
    } 

    // rest of action 
} 

답변

1

Antiforgery 토큰 검증이 자동으로 필터가 컨트롤러 속성을 사용하여 수행 할 수 있습니다.

  • [AutoValidateAntiforgeryToken]을 사용하여 모든 "안전하지 않은"방법에서 토큰의 유효성을 검사하십시오. (GET, HEAD, TRACE, OPTIONS 이외의 메소드).
  • 사용 [ValidateAntiforgeryToken]은 항상 당신이 필요로하는 단위를 달성하기 위해 이러한 특성을 결합 할 수 있습니다 토큰 검증을

을 무시하는 토큰

  • 사용 [IgnoreAntiforgeryToken]을 확인합니다. 예를 들어 :

    //Validate all 'unsafe' actions except the ones with the ignore attribute 
    [AutoValidateAntiforgeryToken] 
    public class MyApi: Controller 
    { 
        [HttpPost] 
        public IActionResult DoSomething(){ } 
        [HttpPut] 
        public IActionResult DoSomethingElse(){ } 
    
        [IgnoreAntiforgeryToken] 
        public IActionResult DoSomethingSafe(){ } 
    } 
    
    //Validate only explicit actions 
    public class ArticlesController: Controller 
    { 
        public IActionResult Index(){ } 
    
        [ValidateAntiforgeryToken] 
        [HttpPost] 
        public IActionResult Create(){ } 
    } 
    

    나는 docs site에서 완벽하게 준비 문서 밤은을 발견했습니다,하지만 당신은 github에 issue에서의 초안을 볼 수 있습니다.

  • 1

    antiforgery 토큰이 자동적으로 생성 FormTagHelper 추가된다. 당신은 asp-antiforgery="true" 속성을 추가하여이 자동 기능을 활성화/비활성화 할 수 있습니다

    <form asp-controller="Account" asp-action="LogOff" asp-antiforgery="true" 
         method="post" id="logoutForm" class="navbar-right"> 
    </form> 
    
    1

    이 Daniel`s 응답을 바탕으로, 내가

    [HttpPost] 
    [AllowAnonymous] 
    [IgnoreAntiforgeryToken] 
    public ActionResult Index() 
    { 
        if (!User.Identity.IsAuthenticated) 
        { 
         return NewIndex(); 
        } 
    
        // rest of action 
    } 
    
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult NewIndex() 
    { 
        // body of new action 
    } 
    

    docs draft에 따라 다른 옵션을 코드를 변경, 서비스로 Antiforgery를 주입입니다.

    Project.json

    "Microsoft.AspNetCore.Antiforgery": "1.0.0" 
    

    Startup.cs는

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery) 
    { 
        ... 
    
    
    public void ConfigureServices(IServiceCollection services) 
    { 
        services.AddAntiforgery(); 
        ... 
    

    나서 검증 컨트롤러.

    public class MyController : Controller 
    { 
        private readonly IAntiforgery _antiforgery; 
    
        public AccountController(IAntiforgery antiforgery) 
        { 
         _antiforgery = antiforgery; 
        } 
    
        public ActionResult Index() 
        { 
         if (!User.Identity.IsAuthenticated) 
         { 
          await _antiforgery.ValidateRequestAsync(HttpContext); 
         } 
    
         // rest of action 
        } 
    
    } 
    
    관련 문제