~ubuntu-branches/ubuntu/lucid/mono/lucid

« back to all changes in this revision

Viewing changes to mcs/class/System.Web.Mvc/System.Web.Mvc/AntiForgeryToken.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2009-07-30 19:35:10 UTC
  • mto: (5.2.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20090730193510-cdttfvqokq2kmdvh
Tags: upstream-2.4.2.3+dfsg
Import upstream version 2.4.2.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace System.Web.Mvc {
 
2
    using System;
 
3
    using System.Security.Cryptography;
 
4
 
 
5
    internal sealed class AntiForgeryToken {
 
6
 
 
7
        private const int TokenLength = 128 / 8;
 
8
        private static RNGCryptoServiceProvider _prng = new RNGCryptoServiceProvider();
 
9
 
 
10
        private string _salt;
 
11
        private string _value;
 
12
 
 
13
        public AntiForgeryToken() {
 
14
        }
 
15
 
 
16
        // copy constructor
 
17
        public AntiForgeryToken(AntiForgeryToken token) {
 
18
            if (token == null) {
 
19
                throw new ArgumentNullException("token");
 
20
            }
 
21
 
 
22
            CreationDate = token.CreationDate;
 
23
            Salt = token.Salt;
 
24
            Value = token.Value;
 
25
        }
 
26
 
 
27
        public DateTime CreationDate {
 
28
            get;
 
29
            set;
 
30
        }
 
31
 
 
32
        public string Salt {
 
33
            get {
 
34
                return _salt ?? String.Empty;
 
35
            }
 
36
            set {
 
37
                _salt = value;
 
38
            }
 
39
        }
 
40
 
 
41
        public string Value {
 
42
            get {
 
43
                return _value ?? String.Empty;
 
44
            }
 
45
            set {
 
46
                _value = value;
 
47
            }
 
48
        }
 
49
 
 
50
        private static string GenerateRandomTokenString() {
 
51
            byte[] tokenBytes = new byte[TokenLength];
 
52
            _prng.GetBytes(tokenBytes);
 
53
 
 
54
            string token = Convert.ToBase64String(tokenBytes);
 
55
            return token;
 
56
        }
 
57
 
 
58
        public static AntiForgeryToken NewToken() {
 
59
            string tokenString = GenerateRandomTokenString();
 
60
            return new AntiForgeryToken() {
 
61
                CreationDate = DateTime.Now,
 
62
                Value = tokenString
 
63
            };
 
64
        }
 
65
 
 
66
    }
 
67
}