runtime loading of ValidateAntiForgeryToken Salt value

前端 未结 2 1182
猫巷女王i
猫巷女王i 2020-12-06 15:21

Consider an ASP.NET MVC application using the Salt parameter in the [ValidateAntiForgeryToken] directive.

The scenario is such that the app

2条回答
  •  天命终不由人
    2020-12-06 15:56

    I had the requirement to have different salts for different customers. In this case, I used Dixin's solution for injecting the salt at runtime.

    Anti Forgery Request Recipes For ASP.NET MVC and AJAX at the section titled "Specify non-constant salt in runtime".

    Decorate your Controllers with a new attribute:

    [ValidateAntiForgeryTokenWrapper(HttpVerbs.Post)]
    public class ProductController : Controller
    {     
        // Only HTTP POST requests are validated.
    }
    

    This new attribute is defined as:

    public class ValidateAntiForgeryTokenWrapperAttribute : FilterAttribute, IAuthorizationFilter
    {
        public ValidateAntiForgeryTokenWrapperAttribute(HttpVerbs verbs)
        {
            this._verbs = new AcceptVerbsAttribute(verbs);
            this._validator = new ValidateAntiForgeryTokenAttribute()
                {
                    //load from web.config or anywhere else
                    Salt = Configurations.AntiForgeryTokenSalt
                };
        }
    
        // Other members.
    }
    

提交回复
热议问题