码迷,mamicode.com
首页 > 其他好文 > 详细

111

时间:2017-05-22 16:39:39      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:访问权限   directory   har   context   for   dom   static   inherits   uri   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.IO; 

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //下面的方法调用时最好做成异步调用,以便在文件过大时不让用户等待过久

            //asp.net模拟域账号访问客户端共享文件夹,报对路径"\\xxx\folder"的访问被拒绝
            //web.config文件system.web节需配置<identity impersonate="true" userName="域\域账号" password="密码" />
            IntPtr ptr = default(IntPtr);
            if (WinLogonHelper.LogonUser("域账号", "H3C.HUAWEI-3COM.COM", "密码", 9, 0, ref ptr) != 0)
            {
                using (WindowsIdentity wi = new WindowsIdentity(ptr))
                {
                    using (WindowsImpersonationContext wic = wi.Impersonate())
                    {
                        if (!Directory.Exists(@"\\\\xxx\\folder"))
                        {
                            //......客户端文件夹对当前域账号开放共享,此处域账号可以根据所开发的权限对文件夹进行访问,例如读取文件夹中的文件
                            //通过文件共享的方式,端到端的文件传输不受文件大小和文件个数制约

                            //将文件从客户端复制到服务端后,可以对新生成的文件目录设置共享访问权限,例如设置只读权限
                            SetFolderACL("文件保存路径", "域\\(一个反斜杠)域账号", FileSystemRights.Read, AccessControlType.Allow);
                        }
                    }
                }
            }
        }

        public static class WinLogonHelper
        {
            [DllImport("advapi32.DLL", SetLastError = true)]
            public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
        }

        public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny)
        {

            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

            return SetFolderACL(FolderPath, UserName, Rights, AllowOrDeny, inherits, PropagationFlags.None, AccessControlModification.Add);

        }

        public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny, InheritanceFlags Inherits, PropagationFlags PropagateToChildren, AccessControlModification AddResetOrRemove)
        {
            bool ret;

            DirectoryInfo folder = new DirectoryInfo(FolderPath);

            DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);

            FileSystemAccessRule accRule = new FileSystemAccessRule(UserName, FileSystemRights.Read, Inherits, PropagateToChildren, AllowOrDeny);
            dSecurity.ModifyAccessRule(AddResetOrRemove, accRule, out ret);

            folder.SetAccessControl(dSecurity);

            DirectorySecurity fs1 = System.IO.Directory.GetAccessControl(FolderPath);
            fs1.SetAccessRuleProtection(false, true);
            System.IO.Directory.SetAccessControl(FolderPath, fs1);

            return ret;
        }
    }
}

  

111

标签:访问权限   directory   har   context   for   dom   static   inherits   uri   

原文地址:http://www.cnblogs.com/Arlar/p/6889823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!