Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / clr / src / BCL / System / Security / Policy / ApplicationSecurityManager.cs / 1 / ApplicationSecurityManager.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// ApplicationSecurityManager.cs
//
namespace System.Security.Policy {
using System.Deployment.Internal.Isolation;
using System.Deployment.Internal.Isolation.Manifest;
using System.IO;
using System.Runtime.Versioning;
using System.Security.Permissions;
using System.Security.Util;
[System.Runtime.InteropServices.ComVisible(true)]
public static class ApplicationSecurityManager {
private static IApplicationTrustManager m_appTrustManager = null;
//
// Public static methods.
//
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlEvidence)]
[SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted=true)]
public static bool DetermineApplicationTrust (ActivationContext activationContext, TrustManagerContext context) {
if (activationContext == null)
throw new ArgumentNullException("activationContext");
ApplicationTrust appTrust = null;
AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
if (domainManager != null) {
HostSecurityManager securityManager = domainManager.HostSecurityManager;
if ((securityManager != null) && ((securityManager.Flags & HostSecurityManagerOptions.HostDetermineApplicationTrust) == HostSecurityManagerOptions.HostDetermineApplicationTrust)) {
appTrust = securityManager.DetermineApplicationTrust(CmsUtils.MergeApplicationEvidence(null, activationContext.Identity, activationContext, null), null, context);
if (appTrust == null)
return false;
return appTrust.IsApplicationTrustedToRun;
}
}
appTrust = DetermineApplicationTrustInternal(activationContext, context);
if (appTrust == null)
return false;
return appTrust.IsApplicationTrustedToRun;
}
//
// Public static properties.
//
public static ApplicationTrustCollection UserApplicationTrusts {
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
get {
return new ApplicationTrustCollection(true);
}
}
public static IApplicationTrustManager ApplicationTrustManager {
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
get {
if (m_appTrustManager == null) {
m_appTrustManager = DecodeAppTrustManager();
if (m_appTrustManager == null)
throw new PolicyException(Environment.GetResourceString("Policy_NoTrustManager"));
}
return m_appTrustManager;
}
}
//
// Internal
//
internal static ApplicationTrust DetermineApplicationTrustInternal (ActivationContext activationContext, TrustManagerContext context) {
ApplicationTrust trust = null;
ApplicationTrustCollection userTrusts = new ApplicationTrustCollection(true);
// See if there is a persisted trust decision for this application.
if ((context == null || !context.IgnorePersistedDecision)) {
trust = userTrusts[activationContext.Identity.FullName];
if (trust != null)
return trust;
}
// There is no cached trust decision so invoke the trust manager.
trust = ApplicationTrustManager.DetermineApplicationTrust(activationContext, context);
if (trust == null)
trust = new ApplicationTrust(activationContext.Identity);
// make sure the application identity is correctly set.
trust.ApplicationIdentity = activationContext.Identity;
if (trust.Persist)
userTrusts.Add(trust);
return trust;
}
//
// Private.
//
private static string s_machineConfigFile = Config.MachineDirectory + "applicationtrust.config";
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
private static IApplicationTrustManager DecodeAppTrustManager () {
if (!File.InternalExists(s_machineConfigFile))
goto defaultTrustManager;
// A config file exists. Decode the trust manager from its Xml.
FileStream contents = new FileStream(s_machineConfigFile, FileMode.Open, FileAccess.Read);
SecurityElement elRoot = SecurityElement.FromString(new StreamReader(contents).ReadToEnd());
SecurityElement elMscorlib = elRoot.SearchForChildByTag("mscorlib");
if (elMscorlib == null)
goto defaultTrustManager;
SecurityElement elSecurity = elMscorlib.SearchForChildByTag("security");
if (elSecurity == null)
goto defaultTrustManager;
SecurityElement elPolicy = elSecurity.SearchForChildByTag("policy");
if (elPolicy == null)
goto defaultTrustManager;
SecurityElement elSecurityManager = elPolicy.SearchForChildByTag("ApplicationSecurityManager");
if (elSecurityManager == null)
goto defaultTrustManager;
SecurityElement elTrustManager = elSecurityManager.SearchForChildByTag("IApplicationTrustManager");
if (elTrustManager == null)
goto defaultTrustManager;
IApplicationTrustManager appTrustManager = DecodeAppTrustManagerFromElement(elTrustManager);
if (appTrustManager == null)
goto defaultTrustManager;
return appTrustManager;
defaultTrustManager:
return DecodeAppTrustManagerFromElement(CreateDefaultApplicationTrustManagerElement());
}
private static SecurityElement CreateDefaultApplicationTrustManagerElement() {
SecurityElement elTrustManager = new SecurityElement("IApplicationTrustManager");
elTrustManager.AddAttribute("class",
"System.Security.Policy.TrustManager, System.Windows.Forms, Version=" + System.Reflection.Assembly.GetExecutingAssembly().GetVersion() + ", Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKeyToken);
elTrustManager.AddAttribute("version", "1");
return elTrustManager;
}
private static IApplicationTrustManager DecodeAppTrustManagerFromElement (SecurityElement elTrustManager) {
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
string trustManagerName = elTrustManager.Attribute("class");
Type tmClass = Type.GetType(trustManagerName, false, false);
if (tmClass == null)
return null;
IApplicationTrustManager appTrustManager = Activator.CreateInstance(tmClass) as IApplicationTrustManager;
if (appTrustManager != null)
appTrustManager.FromXml(elTrustManager);
return appTrustManager;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// ApplicationSecurityManager.cs
//
namespace System.Security.Policy {
using System.Deployment.Internal.Isolation;
using System.Deployment.Internal.Isolation.Manifest;
using System.IO;
using System.Runtime.Versioning;
using System.Security.Permissions;
using System.Security.Util;
[System.Runtime.InteropServices.ComVisible(true)]
public static class ApplicationSecurityManager {
private static IApplicationTrustManager m_appTrustManager = null;
//
// Public static methods.
//
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlEvidence)]
[SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted=true)]
public static bool DetermineApplicationTrust (ActivationContext activationContext, TrustManagerContext context) {
if (activationContext == null)
throw new ArgumentNullException("activationContext");
ApplicationTrust appTrust = null;
AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
if (domainManager != null) {
HostSecurityManager securityManager = domainManager.HostSecurityManager;
if ((securityManager != null) && ((securityManager.Flags & HostSecurityManagerOptions.HostDetermineApplicationTrust) == HostSecurityManagerOptions.HostDetermineApplicationTrust)) {
appTrust = securityManager.DetermineApplicationTrust(CmsUtils.MergeApplicationEvidence(null, activationContext.Identity, activationContext, null), null, context);
if (appTrust == null)
return false;
return appTrust.IsApplicationTrustedToRun;
}
}
appTrust = DetermineApplicationTrustInternal(activationContext, context);
if (appTrust == null)
return false;
return appTrust.IsApplicationTrustedToRun;
}
//
// Public static properties.
//
public static ApplicationTrustCollection UserApplicationTrusts {
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
get {
return new ApplicationTrustCollection(true);
}
}
public static IApplicationTrustManager ApplicationTrustManager {
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
get {
if (m_appTrustManager == null) {
m_appTrustManager = DecodeAppTrustManager();
if (m_appTrustManager == null)
throw new PolicyException(Environment.GetResourceString("Policy_NoTrustManager"));
}
return m_appTrustManager;
}
}
//
// Internal
//
internal static ApplicationTrust DetermineApplicationTrustInternal (ActivationContext activationContext, TrustManagerContext context) {
ApplicationTrust trust = null;
ApplicationTrustCollection userTrusts = new ApplicationTrustCollection(true);
// See if there is a persisted trust decision for this application.
if ((context == null || !context.IgnorePersistedDecision)) {
trust = userTrusts[activationContext.Identity.FullName];
if (trust != null)
return trust;
}
// There is no cached trust decision so invoke the trust manager.
trust = ApplicationTrustManager.DetermineApplicationTrust(activationContext, context);
if (trust == null)
trust = new ApplicationTrust(activationContext.Identity);
// make sure the application identity is correctly set.
trust.ApplicationIdentity = activationContext.Identity;
if (trust.Persist)
userTrusts.Add(trust);
return trust;
}
//
// Private.
//
private static string s_machineConfigFile = Config.MachineDirectory + "applicationtrust.config";
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
private static IApplicationTrustManager DecodeAppTrustManager () {
if (!File.InternalExists(s_machineConfigFile))
goto defaultTrustManager;
// A config file exists. Decode the trust manager from its Xml.
FileStream contents = new FileStream(s_machineConfigFile, FileMode.Open, FileAccess.Read);
SecurityElement elRoot = SecurityElement.FromString(new StreamReader(contents).ReadToEnd());
SecurityElement elMscorlib = elRoot.SearchForChildByTag("mscorlib");
if (elMscorlib == null)
goto defaultTrustManager;
SecurityElement elSecurity = elMscorlib.SearchForChildByTag("security");
if (elSecurity == null)
goto defaultTrustManager;
SecurityElement elPolicy = elSecurity.SearchForChildByTag("policy");
if (elPolicy == null)
goto defaultTrustManager;
SecurityElement elSecurityManager = elPolicy.SearchForChildByTag("ApplicationSecurityManager");
if (elSecurityManager == null)
goto defaultTrustManager;
SecurityElement elTrustManager = elSecurityManager.SearchForChildByTag("IApplicationTrustManager");
if (elTrustManager == null)
goto defaultTrustManager;
IApplicationTrustManager appTrustManager = DecodeAppTrustManagerFromElement(elTrustManager);
if (appTrustManager == null)
goto defaultTrustManager;
return appTrustManager;
defaultTrustManager:
return DecodeAppTrustManagerFromElement(CreateDefaultApplicationTrustManagerElement());
}
private static SecurityElement CreateDefaultApplicationTrustManagerElement() {
SecurityElement elTrustManager = new SecurityElement("IApplicationTrustManager");
elTrustManager.AddAttribute("class",
"System.Security.Policy.TrustManager, System.Windows.Forms, Version=" + System.Reflection.Assembly.GetExecutingAssembly().GetVersion() + ", Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKeyToken);
elTrustManager.AddAttribute("version", "1");
return elTrustManager;
}
private static IApplicationTrustManager DecodeAppTrustManagerFromElement (SecurityElement elTrustManager) {
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
string trustManagerName = elTrustManager.Attribute("class");
Type tmClass = Type.GetType(trustManagerName, false, false);
if (tmClass == null)
return null;
IApplicationTrustManager appTrustManager = Activator.CreateInstance(tmClass) as IApplicationTrustManager;
if (appTrustManager != null)
appTrustManager.FromXml(elTrustManager);
return appTrustManager;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XNameConverter.cs
- DataGridViewRow.cs
- UnknownWrapper.cs
- OuterGlowBitmapEffect.cs
- XamlReader.cs
- PreviewControlDesigner.cs
- DataGridViewTextBoxCell.cs
- Error.cs
- SmiGettersStream.cs
- ToolStripDesignerAvailabilityAttribute.cs
- XamlNamespaceHelper.cs
- ISCIIEncoding.cs
- EnumBuilder.cs
- CompiledIdentityConstraint.cs
- StateItem.cs
- httpstaticobjectscollection.cs
- TransformerTypeCollection.cs
- Vector3DAnimationBase.cs
- ListDictionary.cs
- ParserHooks.cs
- HostProtectionException.cs
- GenerateTemporaryAssemblyTask.cs
- AssemblyNameProxy.cs
- OleAutBinder.cs
- TemplateBamlTreeBuilder.cs
- SetUserPreferenceRequest.cs
- PropertyGridView.cs
- ToolboxDataAttribute.cs
- CompositionAdorner.cs
- HttpWebRequestElement.cs
- HttpRawResponse.cs
- SingleStorage.cs
- LayoutEditorPart.cs
- MDIWindowDialog.cs
- _ScatterGatherBuffers.cs
- ProjectionPathBuilder.cs
- TextSegment.cs
- CodeDomConfigurationHandler.cs
- HttpValueCollection.cs
- ToolStripDesignerAvailabilityAttribute.cs
- ExceptionWrapper.cs
- DesignerCalendarAdapter.cs
- DoubleSumAggregationOperator.cs
- ButtonField.cs
- IndicFontClient.cs
- NodeInfo.cs
- RenderTargetBitmap.cs
- CommandSet.cs
- Int64Animation.cs
- PackUriHelper.cs
- Configuration.cs
- EndpointReference.cs
- DBConnectionString.cs
- TypeGeneratedEventArgs.cs
- ObjectFactoryCodeDomTreeGenerator.cs
- FileDialogCustomPlace.cs
- RequestTimeoutManager.cs
- DbProviderManifest.cs
- CompositeCollection.cs
- AlternationConverter.cs
- MouseButtonEventArgs.cs
- WindowsFormsHostPropertyMap.cs
- TextDecorationCollection.cs
- SafeRegistryHandle.cs
- Table.cs
- EditorPartCollection.cs
- ImportContext.cs
- EntityRecordInfo.cs
- DeliveryStrategy.cs
- GenerateScriptTypeAttribute.cs
- PointAnimationClockResource.cs
- PathSegmentCollection.cs
- ByteStream.cs
- DataGridViewMethods.cs
- FieldBuilder.cs
- WmlMobileTextWriter.cs
- FunctionNode.cs
- NetWebProxyFinder.cs
- WmlLinkAdapter.cs
- HostingEnvironmentSection.cs
- MarkupCompilePass2.cs
- WebServiceEnumData.cs
- StackOverflowException.cs
- EventLogPermissionEntry.cs
- SaveFileDialog.cs
- MessagePropertyFilter.cs
- XsltException.cs
- bidPrivateBase.cs
- WebServiceClientProxyGenerator.cs
- MediaTimeline.cs
- MediaContextNotificationWindow.cs
- InheritanceAttribute.cs
- CryptoHelper.cs
- CodeAttachEventStatement.cs
- DataGridLinkButton.cs
- CompensateDesigner.cs
- ExitEventArgs.cs
- ListViewGroupCollectionEditor.cs
- UDPClient.cs
- XamlTypeMapperSchemaContext.cs