0

直到今天,我为我的社会 Intranet 使用了窗口身份验证(内置于 asp mvc 3.0)。但是,因为急需用户登录,我只好通过表单认证。

现在一切都很好,除了我用来探索服务器中目录的一个功能。根据用户权限,用户可以读取一些目录和文件。这是我的功能:

public static MvcHtmlString Explore()
    {
        WindowsIdentity id = (WindowsIdentity)HttpContext.Current.User.Identity;

        MvcHtmlString s = null;
        using (System.Security.Principal.WindowsImpersonationContext context = System.Security.Principal.WindowsIdentity.Impersonate(id.Token))
        {
            try
            {
                s = new MvcHtmlString(Explore(documentsRootFolder).ToString());
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write(e.Message+"<br/>");
                HttpContext.Current.Response.Write(e.StackTrace);
            }
        }
        return s;
    }

    private static StringBuilder Explore(string path)
    {

        StringBuilder writer = new StringBuilder();
        writer.Append("<ul>");
        try
        {
            foreach (var a in System.IO.Directory.GetDirectories(path))
            {
                writer.AppendFormat("<li>{0}</li>", a.Replace((path.EndsWith(@"\") ? path : path+@"\"), string.Empty));
                writer.Append(Explore(a));

            }

            foreach (var a in System.IO.Directory.GetFiles(path))
            {
                string url = a.Replace(documentsRootFolder, string.Empty).Replace(@"\", "/");
                string friendlyName = a.Replace((path.EndsWith(@"\") ? path : path + @"\"), string.Empty);
                writer.AppendFormat("<li><a href=\"Open?path={0}\">{1}</a></li>", url, friendlyName);
            }
        }
        catch { }
        writer.Append("</ul>");
        return writer;
    }

当然,通过表单身份验证,我无法从 HttpContext 用户获取 Windows 身份。我现在如何读取目录和文件?

PS:我尝试使用 advapi32.dll 来获取用户的 windows 身份。但是,登录后,如果不关闭浏览器(ssi)就无法注销。这就是我寻找另一个解决方案的原因。是否可以在不登录的情况下获得 Windows 身份令牌?

谢谢

4

1 回答 1

1

可能不安全,也不推荐。但是您可以将用户名和密码保存在 Session var 或其他内容中。然后在需要时通过此类使用 Impersonation 。

using(new Impersonation("username","domain","password"))
{
  s = new MvcHtmlString(Explore(documentsRootFolder).ToString());
}
于 2014-05-15T16:03:32.233 回答