Moving BSTR conv to native code in SecureStringToBSTR.
[mono.git] / mcs / class / referencesource / System.Web / Security / FormsIdentity.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="FormsIdentity.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  * FormsIdentity
9  *
10  * Copyright (c) 1999 Microsoft Corporation
11  */
12
13 namespace System.Web.Security {
14     using System.Collections.Generic;
15     using System.Runtime.InteropServices;
16     using System.Runtime.Serialization;
17     using System.Security;
18     using System.Security.Claims;
19     using System.Security.Permissions;
20
21     /// <devdoc>
22     ///    This class is an IIdentity derived class
23     ///    used by FormsAuthenticationModule. It provides a way for an application to
24     ///    access the cookie authentication ticket.
25     /// </devdoc>
26     [Serializable]
27     [ComVisible(false)]
28     public class FormsIdentity : ClaimsIdentity {
29       
30         /// <devdoc>
31         ///    The name of the identity (in this case, the
32         ///    passport user name).
33         /// </devdoc>
34         public  override String                       Name { get { return _Ticket.Name;}}
35
36         /// <devdoc>
37         ///    The type of the identity (in this case,
38         ///    "Forms").
39         /// </devdoc>
40         public  override String                       AuthenticationType { get { return "Forms";}}
41
42         /// <devdoc>
43         ///    Indicates whether or not authentication took
44         ///    place.
45         /// </devdoc>
46         public  override bool                         IsAuthenticated { get { return true;}}
47         
48         private FormsAuthenticationTicket _Ticket;
49
50         /// <devdoc>
51         ///    Returns the FormsAuthenticationTicket
52         ///    associated with the current request.
53         /// </devdoc>
54         public  FormsAuthenticationTicket   Ticket { get { return _Ticket;}}
55
56         public override IEnumerable<Claim> Claims
57         {
58             get
59             {
60                 return base.Claims;
61             }
62         }
63
64         /// <devdoc>
65         ///    Constructor.
66         /// </devdoc>
67         public FormsIdentity (FormsAuthenticationTicket ticket) {
68             if (ticket == null)
69                 throw new ArgumentNullException("ticket");
70             
71             _Ticket = ticket;
72
73             AddNameClaim();
74         }
75
76         /// <devdoc>
77         ///    Constructor.
78         /// </devdoc>
79         protected FormsIdentity(FormsIdentity identity)
80             : base(identity)
81         {
82             _Ticket = identity._Ticket;
83         }
84
85         /// <devdoc>
86         /// Returns a new instance of <see cref="FormsIdentity"/> with values copied from this object.
87         /// </devdoc>
88         public override ClaimsIdentity Clone()
89         {
90             return new FormsIdentity(this);
91         }
92
93         [OnDeserialized()]
94         private void OnDeserializedMethod(StreamingContext context)
95         {
96             // FormIdentities that have been deserialized from a .net 4.0 runtime, will not have any claims. 
97             // In this case add a name claim, otherwise assume it was deserialized.
98
99             bool claimFound = false;
100             foreach (Claim c in base.Claims)
101             {
102                 claimFound = true;
103                 break;
104             }
105
106             if (!claimFound)
107             {
108                 AddNameClaim();
109             }
110         }
111
112         [SecuritySafeCritical]
113         private void AddNameClaim()
114         {
115             if (_Ticket != null && _Ticket.Name != null)
116             {
117                 base.AddClaim(new Claim(base.NameClaimType, _Ticket.Name, ClaimValueTypes.String, "Forms", "Forms", this));
118             }
119         }
120     }
121 }