|
1 | | -using System.Web.WebPages.Resources; |
| 1 | +using System.ComponentModel; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Web.Helpers.AntiXsrf; |
| 4 | +using System.Web.Mvc; |
| 5 | +using System.Web.WebPages.Resources; |
2 | 6 |
|
3 | 7 | namespace System.Web.Helpers |
4 | 8 | { |
| 9 | + /// <summary> |
| 10 | + /// Provides access to the anti-forgery system, which provides protection against |
| 11 | + /// Cross-site Request Forgery (XSRF, also called CSRF) attacks. |
| 12 | + /// </summary> |
5 | 13 | public static class AntiForgery |
6 | 14 | { |
7 | 15 | private static readonly AntiForgeryWorker _worker = new AntiForgeryWorker(); |
8 | 16 |
|
| 17 | + /// <summary> |
| 18 | + /// Generates an anti-forgery token for this request. This token can |
| 19 | + /// be validated by calling the Validate() method. |
| 20 | + /// </summary> |
| 21 | + /// <returns>An HTML string corresponding to an <input type="hidden"> |
| 22 | + /// element. This element should be put inside a <form>.</returns> |
| 23 | + /// <remarks> |
| 24 | + /// This method has a side effect: it may set a response cookie. |
| 25 | + /// </remarks> |
9 | 26 | public static HtmlString GetHtml() |
10 | 27 | { |
11 | 28 | if (HttpContext.Current == null) |
12 | 29 | { |
13 | 30 | throw new ArgumentException(WebPageResources.HttpContextUnavailable); |
14 | 31 | } |
15 | 32 |
|
16 | | - return GetHtml(new HttpContextWrapper(HttpContext.Current), salt: null, domain: null, path: null); |
| 33 | + TagBuilder retVal = _worker.GetFormInputElement(new HttpContextWrapper(HttpContext.Current)); |
| 34 | + return retVal.ToHtmlString(TagRenderMode.SelfClosing); |
17 | 35 | } |
18 | 36 |
|
| 37 | + /// <summary> |
| 38 | + /// Generates an anti-forgery token pair (cookie and form token) for this request. |
| 39 | + /// This method is similar to GetHtml(), but this method gives the caller control |
| 40 | + /// over how to persist the returned values. To validate these tokens, call the |
| 41 | + /// appropriate overload of Validate. |
| 42 | + /// </summary> |
| 43 | + /// <param name="oldCookieToken">The anti-forgery token - if any - that already existed |
| 44 | + /// for this request. May be null. The anti-forgery system will try to reuse this cookie |
| 45 | + /// value when generating a matching form token.</param> |
| 46 | + /// <param name="newCookieToken">Will contain a new cookie value if the old cookie token |
| 47 | + /// was null or invalid. If this value is non-null when the method completes, the caller |
| 48 | + /// must persist this value in the form of a response cookie, and the existing cookie value |
| 49 | + /// should be discarded. If this value is null when the method completes, the existing |
| 50 | + /// cookie value was valid and needn't be modified.</param> |
| 51 | + /// <param name="formToken">The value that should be stored in the <form>. The caller |
| 52 | + /// should take care not to accidentally swap the cookie and form tokens.</param> |
| 53 | + /// <remarks> |
| 54 | + /// Unlike the GetHtml() method, this method has no side effect. The caller |
| 55 | + /// is responsible for setting the response cookie and injecting the returned |
| 56 | + /// form token as appropriate. |
| 57 | + /// </remarks> |
| 58 | + [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "1#", Justification = "Method is intended for advanced audiences.")] |
| 59 | + [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#", Justification = "Method is intended for advanced audiences.")] |
| 60 | + [EditorBrowsable(EditorBrowsableState.Advanced)] |
| 61 | + public static void GetTokens(string oldCookieToken, out string newCookieToken, out string formToken) |
| 62 | + { |
| 63 | + if (HttpContext.Current == null) |
| 64 | + { |
| 65 | + throw new ArgumentException(WebPageResources.HttpContextUnavailable); |
| 66 | + } |
| 67 | + |
| 68 | + _worker.GetTokens(new HttpContextWrapper(HttpContext.Current), oldCookieToken, out newCookieToken, out formToken); |
| 69 | + } |
| 70 | + |
| 71 | + [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "AdditionalDataProvider", Justification = "API name.")] |
| 72 | + [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "AntiForgeryConfig", Justification = "API name.")] |
| 73 | + [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "GetHtml", Justification = "API name.")] |
| 74 | + [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "httpCookies", Justification = "API name.")] |
| 75 | + [Obsolete("This method is deprecated. Use the GetHtml() method instead. To specify a custom domain for the generated cookie, use the <httpCookies> configuration element. To specify custom data to be embedded within the token, use the static AntiForgeryConfig.AdditionalDataProvider property.", error: true)] |
| 76 | + [EditorBrowsable(EditorBrowsableState.Never)] |
19 | 77 | public static HtmlString GetHtml(HttpContextBase httpContext, string salt, string domain, string path) |
20 | 78 | { |
21 | 79 | if (httpContext == null) |
22 | 80 | { |
23 | 81 | throw new ArgumentNullException("httpContext"); |
24 | 82 | } |
25 | 83 |
|
26 | | - return _worker.GetHtml(httpContext, salt, domain, path); |
| 84 | + if (!String.IsNullOrEmpty(salt) || !String.IsNullOrEmpty(domain) || !String.IsNullOrEmpty(path)) |
| 85 | + { |
| 86 | + throw new NotSupportedException("This method is deprecated. Use the GetHtml() method instead. To specify a custom domain for the generated cookie, use the <httpCookies> configuration element. To specify custom data to be embedded within the token, use the static AntiForgeryConfig.AdditionalDataProvider property."); |
| 87 | + } |
| 88 | + |
| 89 | + TagBuilder retVal = _worker.GetFormInputElement(httpContext); |
| 90 | + return retVal.ToHtmlString(TagRenderMode.SelfClosing); |
27 | 91 | } |
28 | 92 |
|
| 93 | + /// <summary> |
| 94 | + /// Validates an anti-forgery token that was supplied for this request. |
| 95 | + /// The anti-forgery token may be generated by calling GetHtml(). |
| 96 | + /// </summary> |
| 97 | + /// <remarks> |
| 98 | + /// Throws an HttpAntiForgeryException if validation fails. |
| 99 | + /// </remarks> |
29 | 100 | public static void Validate() |
30 | 101 | { |
31 | 102 | if (HttpContext.Current == null) |
32 | 103 | { |
33 | 104 | throw new ArgumentException(WebPageResources.HttpContextUnavailable); |
34 | 105 | } |
35 | | - Validate(new HttpContextWrapper(HttpContext.Current), salt: null); |
| 106 | + |
| 107 | + _worker.Validate(new HttpContextWrapper(HttpContext.Current)); |
36 | 108 | } |
37 | 109 |
|
| 110 | + /// <summary> |
| 111 | + /// Validates an anti-forgery token pair that was generated by the GetTokens method. |
| 112 | + /// </summary> |
| 113 | + /// <param name="cookieToken">The token that was supplied in the request cookie.</param> |
| 114 | + /// <param name="formToken">The token that was supplied in the request form body.</param> |
| 115 | + /// <remarks> |
| 116 | + /// Throws an HttpAntiForgeryException if validation fails. |
| 117 | + /// </remarks> |
| 118 | + [EditorBrowsable(EditorBrowsableState.Advanced)] |
| 119 | + public static void Validate(string cookieToken, string formToken) |
| 120 | + { |
| 121 | + if (HttpContext.Current == null) |
| 122 | + { |
| 123 | + throw new ArgumentException(WebPageResources.HttpContextUnavailable); |
| 124 | + } |
| 125 | + |
| 126 | + _worker.Validate(new HttpContextWrapper(HttpContext.Current), cookieToken, formToken); |
| 127 | + } |
| 128 | + |
| 129 | + [Obsolete("This method is deprecated. Use the Validate() method instead.", error: true)] |
| 130 | + [EditorBrowsable(EditorBrowsableState.Never)] |
38 | 131 | public static void Validate(HttpContextBase httpContext, string salt) |
39 | 132 | { |
40 | 133 | if (httpContext == null) |
41 | 134 | { |
42 | 135 | throw new ArgumentNullException("httpContext"); |
43 | 136 | } |
44 | 137 |
|
45 | | - _worker.Validate(httpContext, salt); |
| 138 | + if (!String.IsNullOrEmpty(salt)) |
| 139 | + { |
| 140 | + throw new NotSupportedException("This method is deprecated. Use the Validate() method instead."); |
| 141 | + } |
| 142 | + |
| 143 | + _worker.Validate(httpContext); |
46 | 144 | } |
47 | 145 | } |
48 | 146 | } |
0 commit comments