1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4 
5 namespace System.IdentityModel.Tokens
6 {
7     using System.Collections.Generic;
8     using System.Collections.ObjectModel;
9     using System.Security.Principal;
10     using System.IdentityModel.Claims;
11     using System.IdentityModel.Policy;
12     using System.IdentityModel.Tokens;
13 
14     public class WindowsSecurityToken : SecurityToken, IDisposable
15     {
16         string authenticationType;
17         string id;
18         DateTime effectiveTime;
19         DateTime expirationTime;
20         WindowsIdentity windowsIdentity;
21         bool disposed = false;
22 
WindowsSecurityToken(WindowsIdentity windowsIdentity)23         public WindowsSecurityToken(WindowsIdentity windowsIdentity)
24             : this(windowsIdentity, SecurityUniqueId.Create().Value)
25         {
26         }
27 
WindowsSecurityToken(WindowsIdentity windowsIdentity, string id)28         public WindowsSecurityToken(WindowsIdentity windowsIdentity, string id)
29             : this(windowsIdentity, id, null)
30         {
31         }
32 
WindowsSecurityToken(WindowsIdentity windowsIdentity, string id, string authenticationType)33         public WindowsSecurityToken(WindowsIdentity windowsIdentity, string id, string authenticationType)
34         {
35             DateTime effectiveTime = DateTime.UtcNow;
36             Initialize( id, authenticationType, effectiveTime, DateTime.UtcNow.AddHours( 10 ), windowsIdentity, true );
37         }
38 
WindowsSecurityToken()39         protected WindowsSecurityToken()
40         {
41         }
42 
Initialize(string id, DateTime effectiveTime, DateTime expirationTime, WindowsIdentity windowsIdentity, bool clone)43         protected void Initialize(string id, DateTime effectiveTime, DateTime expirationTime, WindowsIdentity windowsIdentity, bool clone)
44         {
45             Initialize( id, null, effectiveTime, expirationTime, windowsIdentity, clone );
46         }
47 
Initialize(string id, string authenticationType, DateTime effectiveTime, DateTime expirationTime, WindowsIdentity windowsIdentity, bool clone)48         protected void Initialize(string id, string authenticationType, DateTime effectiveTime, DateTime expirationTime, WindowsIdentity windowsIdentity, bool clone)
49         {
50 
51             if (windowsIdentity == null)
52                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("windowsIdentity");
53 
54             if (id == null)
55                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("id");
56 
57             this.id = id;
58             this.authenticationType = authenticationType;
59             this.effectiveTime = effectiveTime;
60             this.expirationTime = expirationTime;
61             this.windowsIdentity = clone ? SecurityUtils.CloneWindowsIdentityIfNecessary(windowsIdentity, authenticationType) : windowsIdentity;
62         }
63 
64         public override string Id
65         {
66             get { return this.id; }
67         }
68 
69         public string AuthenticationType
70         {
71             get { return this.authenticationType; }
72         }
73 
74         public override DateTime ValidFrom
75         {
76             get { return this.effectiveTime; }
77         }
78 
79         public override DateTime ValidTo
80         {
81             get { return this.expirationTime; }
82         }
83 
84         public virtual WindowsIdentity WindowsIdentity
85         {
86             get
87             {
88                 ThrowIfDisposed();
89                 return this.windowsIdentity;
90             }
91         }
92 
93         public override ReadOnlyCollection<SecurityKey> SecurityKeys
94         {
95             get { return EmptyReadOnlyCollection<SecurityKey>.Instance; }
96         }
97 
Dispose()98         public virtual void Dispose()
99         {
100             if (!this.disposed)
101             {
102                 this.disposed = true;
103                 if (this.windowsIdentity != null)
104                 {
105                     this.windowsIdentity.Dispose();
106                     this.windowsIdentity = null;
107                 }
108             }
109         }
110 
ThrowIfDisposed()111         protected void ThrowIfDisposed()
112         {
113             if (this.disposed)
114             {
115                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().FullName));
116             }
117         }
118     }
119 }
120