国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 松滋市| 宿松县| 左权县| 将乐县| 浮山县| 梓潼县| 苏尼特右旗| 安泽县| 吴川市| 瑞安市| 博白县| 冕宁县| 峨山| 余干县| 淮北市| 密山市| 大庆市| 辉南县| 嘉义市| 濮阳县| 房产| 黄梅县| 闸北区| 日喀则市| 陆川县| 贵州省| 千阳县| 台东县| 沂源县| 茂名市| 张家界市| 卢湾区| 酒泉市| 蓝田县| 南平市| 呼和浩特市| 玛沁县| 安阳市| 玛纳斯县| 郎溪县| 琼海市|