关闭

C#登录远程windows系统示例

标签: windowsc#applicationuploadstringdictionary
756人阅读 评论(1) 收藏 举报
public class Global : System.Web.HttpApplication
    
{
        
protected void Application_Start(object sender, EventArgs e)
        
{
            System.Runtime.Remoting.RemotingConfiguration.Configure(Server.MapPath(HttpContext.Current.Request.ApplicationPath 
+ "/web.config"), false);

            Application[
"helper"= new Helper();

            
string password = "";

                IntPtr accessToken 
= new IntPtr(0);
                SimpleCrypto.PasswordDecrypt(ConfigurationManager.AppSettings[
"UPLOAD_PASSWD"], out password);
                
if (NetworkSecurity.LogonAndGetToken(ConfigurationManager.AppSettings["UPLOAD_SITE"],
                    ConfigurationManager.AppSettings[
"UPLOAD_USER"], password,
                    ConfigurationManager.AppSettings[
"UPLOAD_NETWORK_LogonType"], ConfigurationManager.AppSettings["UPLOAD_NETWORK_LogonProvider"],
                    
ref accessToken) == false)
                
{
                    
throw new Exception("Logon Failed");
                }

                password 
= "";
                Application.Add(
"AccessToken", accessToken);

                HttpContext.Current.User 
= new WindowsPrincipal(new WindowsIdentity((IntPtr)Application["AccessToken"]));
                (HttpContext.Current.User.Identity 
as WindowsIdentity).Impersonate();
                FileStream fs 
= new FileStream(ConfigurationManager.AppSettings["UPLOADSGFT"], FileMode.Open, FileAccess.Read);
                StreamReader st 
= new StreamReader(fs, System.Text.Encoding.Default);
                
string word;
                Dictionary
<stringstring> postFilter = new Dictionary<stringstring>();
                
while ((word = st.ReadLine()) != null)
                
{
                    
if (!word.Trim().StartsWith("//"&& !postFilter.ContainsKey(word.Trim().ToLower()))
                        postFilter.Add(word.Trim().ToLower(), 
string.Empty);
                }

                st.Close();
                Application.Add(
"FilterText", postFilter);

        }


        
protected void Application_End(object sender, EventArgs e)
        
{
            NetworkSecurity.CloseToken((IntPtr)Application[
"AccessToken"]);
            Application.Clear();
        }

    }
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43489次
    • 积分:513
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:17篇
    • 译文:0篇
    • 评论:34条
    最新评论