c# - AntiForgery cookie in MVC3 -
i wanted implement this solution handle antiforgery in ajax requests. know there other solutions 1 most.
the problem have deal system.web.webpages 1.0 cannot make use of antiforgeryconfig.cookiename in code.
public override void onauthorization(authorizationcontext filtercontext) { var request = filtercontext.httpcontext.request; // validate posts if (request.httpmethod == webrequestmethods.http.post) { // ajax posts , normal form posts have treated differently when comes // validating antiforgerytoken if (request.isajaxrequest()) { string cookiename = antiforgerydata.getantiforgerytokenname(context.request.applicationpath); var antiforgerycookie = request.cookies[antiforgeryconfig.cookiename]; var cookievalue = antiforgerycookie != null ? antiforgerycookie.value : null; antiforgery.validate(cookievalue, request.headers["__requestverificationtoken"]); } else { new validateantiforgerytokenattribute() .onauthorization(filtercontext); } } }
how can retrieve (programmatically) cookie name set antiforgery system in mvc3? suspect antiforgery.validate part problem i'll handle before. thoughts?
the actual cookie name starts "__requestverificationtoken"
suffix. can find cookie this:
private static string findcookievaluebyname(httprequestbase request) { return request.cookies .cast<string>() .where(cn => cn.startswith("__requestverificationtoken", stringcomparison.ordinalignorecase)) .select(cn => request.cookies[cn].value) .firstordefault(); }
Comments
Post a Comment