.Net应用程序域

一、什么是应用程序域?

    应用程序域是一个逻辑容器,它允许多个程序集在同一个进程内进行,但会组织它们直接访问属于其他程序的内存,此外应用程序域还提供了错误隔离机制,这是因为未处理的异常不会影响到其他应用程序域。所以在其他应用程序域的应用程序可以不受干扰地继续运行!
 
使用应用程序域有什么好处?
    1.应用程序域的优势在于,每个应用程序都可以分配一个不同的安全访问级别,(即使它和其他应用程序域都运行在一个进程中。)
    2.应用程序域比进程更高效,允许多个程序集运行在各自的独立应用程序域中,就省去了启动独立进程的开销!
.net framework 运行时管理应用程序域,然而进程却是由操作系统来管理的。
 
如何创建应用程序域?
    示例:
  1. AppDomain dm =AppDomain.CreateDomain("NewDomain");
  2. Console.WriteLine("Host domain:"+AppDomain.CurrentDomain.FriendlyName);
  3. Console.WriteLine("Child domain:"+ dm.FriendlyName);
如何将程序集加载到应用程序域中?
  1. AppDomain dm =AppDomain.CreateDomain("NewDomain");
  2. dm.ExecuteAssembly("Assembly.exe");
   1. AppDomain.ExecuteAssembly的方法重载还允许传递命令行参数,除了提供程序集的完整路径之外,还可以添加该程序集的引用,然后使用AppDomain.ExecuteAssemblyByName方法并传递名称:
  1. AppDomain dm =AppDomain.CreateDomain("NewDomain");
  2. dm.ExecuteAssemblyByName("Assembly");
如何卸载应用程序域?
  1. AppDomain dm =AppDomain.CreateDomain("NewDomain");
  2. AppDomain.Unload(dm);
单独的程序集或类型不能被卸载!

二、配置应用程序域。

    1.为程序集提供宿主机证据?
        证据是指由运行时聚集的关于一个程序集的信息,用来确定该程序集属于哪个代码组织,代码反过来又决定了程序集的特权。
    2.在应用程序域中控制分配给程序集权限最简单发方法就是使用System.Security.Policy.Zone 对象
  1. object[] hostEvidence ={newSystem.Security.Policy.Zone(System.Security.SecurityZone.Internet)};
  2. System.Security.Policy.EvidenceInternetEvidence=newSystem.Security.Policy.Evidence(hostEvidence,null);
  3. AppDomain dom =AppDomain.CreateDomain("MyDomain");
  4. dom.ExecuteAssembly("SecondAssembly.exe",InternetEvidence);
 
    
 





转载于:https://www.cnblogs.com/BookCode/p/5949475.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 自动登录(AD自动登录)可以让用户在访问Web应用程序时无需再次输入他们的凭据。它允许用户在他们的计算机上通过Windows身份验证登录,并自动通过ASP.NET应用程序进行身份验证。 以下是实现ASP.NET自动登录(AD自动登录)的步骤: 1. 在Web.config文件中启用Windows身份验证: ``` <authentication mode="Windows" /> ``` 2. 在Global.asax文件中添加以下代码: ``` protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is WindowsIdentity) { // Get the Windows identity. var windowsIdentity = (WindowsIdentity)HttpContext.Current.User.Identity; // Get the user name and domain name. var userName = windowsIdentity.Name; var domainName = windowsIdentity.Name.Split('\\')[0]; // Authenticate the user against Active Directory. if (AuthenticateUser(userName, domainName)) { // Create a new identity using the user name and domain name. var identity = new GenericIdentity(userName, "Windows"); // Get the roles for the user from Active Directory. var roles = GetRolesForUser(userName, domainName); // Attach the roles to the identity. HttpContext.Current.User = new GenericPrincipal(identity, roles); } else { // Redirect the user to the login page. FormsAuthentication.RedirectToLoginPage(); } } } } } private bool AuthenticateUser(string userName, string domainName) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } private string[] GetRolesForUser(string userName, string domainName) { // Get the roles for the user from Active Directory. // Return an array of role names. } ``` 3. 在Web应用程序中创建一个登录页面。 4. 在登录页面中添加以下代码: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.IsAuthenticated) { // Redirect the user to the default page. Response.Redirect("~/Default.aspx"); } } } protected void btnLogin_Click(object sender, EventArgs e) { // Authenticate the user against Active Directory. if (AuthenticateUser(txtUserName.Text, txtPassword.Text)) { // Redirect the user to the default page. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); } else { // Display an error message. lblErrorMessage.Text = "Invalid username or password."; } } private bool AuthenticateUser(string userName, string password) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } ``` 这些步骤将帮助您实现ASP.NET自动登录(AD自动登录)。当用户访问您的Web应用程序时,他们将通过Windows身份验证自动登录,并自动通过ASP.NET应用程序进行身份验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值