国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ASP.NET模擬指定帳號權限, 刪除非本機圖片文件

2019-11-17 04:07:05
字體:
來源:轉載
供稿:網友
 說明: 執(zhí)行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。

異常詳細信息: System.ArgumentException: 不支持 URI 格式。

源錯誤:

行 30:     PRotected void BTNCLick(object sender, EventArgs e)
行 31:     {
行 32:         System.IO.File.Delete(path + "200911313360.jpg");
行 33:         ScriptManager.RegisterStartupScript(Page, typeof(Page), "ok", "alert('刪除成功');", true);
行 34:     }


不支持 URI 格式。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.Label lbExist;
        protected System.Web.UI.WebControls.Label Label2;
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        WindowsImpersonationContext impersonationContext;  
        [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
        public static extern int LogonUser(String lpszUserName,String lpszDomain,
        String lpszPassWord,int dwLogonType,int dwLogonProvider,
        ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto,  
        SetLastError=true)]
        public extern static int DuplicateToken(IntPtr hToken,int impersonationLevel, ref IntPtr hNewToken);
         
        private void Page_Load(object sender, System.EventArgs e) {
            //noImpersonate();
            //ImpersonateIIS();
            ImpersonateUser();
        }
         
        private void noImpersonate() {
            try
            {
                if(File.Exists("c://Documents and Settings//shaozhidong//test.txt"))
                    lbExist.Text = "存在!";
                else
                    lbExist.Text = "該文件不存在!";
            }
            catch(Exception) {
                lbExist.Text = "沒有權限!";
            }
        }
         
        /// <summary>
        /// 說明:.
        /// 函數:ImpersonateIIS
        /// 時間:2009-11-4  
        /// 作者:zmh_27@126.com
        /// </summary>
        private void ImpersonateIIS() {
            // 在代碼中模擬IIS認證帳號
            System.Security.Principal.WindowsImpersonationContext impersonationContext;
            impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

            if(File.Exists("c://Documents and Settings//shaozhidong//test.txt"))
            lbExist.Text = "存在!";
            else
            lbExist.Text = "該文件不存在!";

            impersonationContext.Undo();
        }

        /// <summary>
        /// 說明:.
        /// 函數:ImpersonateUser
        /// 時間:2009-11-4  
        /// 作者:zmh_27@126.com
        /// </summary>
        private void ImpersonateUser() {
            //在代碼中模擬指定賬號
            if(impersonateValidUser("shaozhidong", "shaozhd", "111")) {    
                if(File.Exists("c://Documents and Settings//shaozhidong//test.txt"))
                    lbExist.Text = "存在!";
                else
                    lbExist.Text = "該文件不存在!";
                undoImpersonation();
            } else {
                lbExist.Text = "權限不夠!";
            }
        }

        private bool impersonateValidUser(string userName, string domain, string password) {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;    
            IntPtr tokenDuplicate = IntPtr.Zero;
            if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,         LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
                if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)  
                        return true;  
                    else
                        return false;   
                } else
                    return false;   
            } else
                return false;
        }

        private void undoImpersonation() {      
            impersonationContext.Undo();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e) {
            //
            // CODEGEN:該調用是 asp.net Web 窗體設計器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        ///  
        /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        ///  
        private void InitializeComponent() {     
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }

}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南城县| 拉萨市| 贵州省| 高安市| 永兴县| 巫山县| 高碑店市| 界首市| 大姚县| 图木舒克市| 沁水县| 双鸭山市| 宁都县| 五河县| 金堂县| 南宁市| 万年县| 阳原县| 鹤山市| 安溪县| 河南省| 蕲春县| 桐城市| 威宁| 金阳县| 阳春市| 湛江市| 丰都县| 始兴县| 广州市| 溧水县| 南乐县| 前郭尔| 大渡口区| 南江县| 深州市| 会同县| 西昌市| 阜阳市| 苍山县| 会昌县|