New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / AntiForgeryData.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Security.Cryptography;\r
16     using System.Security.Principal;\r
17     using System.Text;\r
18 \r
19     internal sealed class AntiForgeryData {\r
20 \r
21         private const string AntiForgeryTokenFieldName = "__RequestVerificationToken";\r
22 \r
23         private const int TokenLength = 128 / 8;\r
24         private readonly static RNGCryptoServiceProvider _prng = new RNGCryptoServiceProvider();\r
25 \r
26         private DateTime _creationDate = DateTime.UtcNow;\r
27         private string _salt;\r
28         private string _username;\r
29         private string _value;\r
30 \r
31         public AntiForgeryData() {\r
32         }\r
33 \r
34         // copy constructor\r
35         public AntiForgeryData(AntiForgeryData token) {\r
36             if (token == null) {\r
37                 throw new ArgumentNullException("token");\r
38             }\r
39 \r
40             CreationDate = token.CreationDate;\r
41             Salt = token.Salt;\r
42             Username = token.Username;\r
43             Value = token.Value;\r
44         }\r
45 \r
46         public DateTime CreationDate {\r
47             get {\r
48                 return _creationDate;\r
49             }\r
50             set {\r
51                 _creationDate = value;\r
52             }\r
53         }\r
54 \r
55         public string Salt {\r
56             get {\r
57                 return _salt ?? String.Empty;\r
58             }\r
59             set {\r
60                 _salt = value;\r
61             }\r
62         }\r
63 \r
64         public string Username {\r
65             get {\r
66                 return _username ?? String.Empty;\r
67             }\r
68             set {\r
69                 _username = value;\r
70             }\r
71         }\r
72 \r
73         public string Value {\r
74             get {\r
75                 return _value ?? String.Empty;\r
76             }\r
77             set {\r
78                 _value = value;\r
79             }\r
80         }\r
81 \r
82         private static string Base64EncodeForCookieName(string s) {\r
83             byte[] rawBytes = Encoding.UTF8.GetBytes(s);\r
84             string base64String = Convert.ToBase64String(rawBytes);\r
85 \r
86             // replace base64-specific characters with characters that are safe for a cookie name\r
87             return base64String.Replace('+', '.').Replace('/', '-').Replace('=', '_');\r
88         }\r
89 \r
90         private static string GenerateRandomTokenString() {\r
91             byte[] tokenBytes = new byte[TokenLength];\r
92             _prng.GetBytes(tokenBytes);\r
93 \r
94             string token = Convert.ToBase64String(tokenBytes);\r
95             return token;\r
96         }\r
97 \r
98         // If the app path is provided, we're generating a cookie name rather than a field name, and the cookie names should\r
99         // be unique so that a development server cookie and an IIS cookie - both running on localhost - don't stomp on\r
100         // each other.\r
101         internal static string GetAntiForgeryTokenName(string appPath) {\r
102             if (String.IsNullOrEmpty(appPath)) {\r
103                 return AntiForgeryTokenFieldName;\r
104             }\r
105             else {\r
106                 return AntiForgeryTokenFieldName + "_" + Base64EncodeForCookieName(appPath);\r
107             }\r
108         }\r
109 \r
110         internal static string GetUsername(IPrincipal user) {\r
111             if (user != null) {\r
112                 IIdentity identity = user.Identity;\r
113                 if (identity != null && identity.IsAuthenticated) {\r
114                     return identity.Name;\r
115                 }\r
116             }\r
117 \r
118             return String.Empty;\r
119         }\r
120 \r
121         public static AntiForgeryData NewToken() {\r
122             string tokenString = GenerateRandomTokenString();\r
123             return new AntiForgeryData() {\r
124                 Value = tokenString\r
125             };\r
126         }\r
127 \r
128     }\r
129 }\r