c#表单身份验证后模拟用户
直到今天,我还在我的社团内部网(内置于asp mvc 3.0)中使用了windowd身份验证。然而,由于迫切需要记录用户的日志,我不得不通过表单身份验证 现在一切都好了,除了我用来浏览服务器目录的一个函数。根据用户权限的不同,用户可以读取一些目录和文件。这是我的职责:c#表单身份验证后模拟用户,c#,asp.net-mvc-3,impersonation,form-authentication,C#,Asp.net Mvc 3,Impersonation,Form Authentication,直到今天,我还在我的社团内部网(内置于asp mvc 3.0)中使用了windowd身份验证。然而,由于迫切需要记录用户的日志,我不得不通过表单身份验证 现在一切都好了,除了我用来浏览服务器目录的一个函数。根据用户权限的不同,用户可以读取一些目录和文件。这是我的职责: public static MvcHtmlString Explore() { WindowsIdentity id = (WindowsIdentity)HttpContext.Current.User.
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;
}
publicstaticmvchtmlstring Explore()
{
WindowsIdentity id=(WindowsIdentity)HttpContext.Current.User.Identity;
MvcHtmlString s=null;
使用(System.Security.Principal.WindowsImpersonationContext=System.Security.Principal.WindowsIdentity.Impersonate(id.Token))
{
尝试
{
s=新的MvcHtmlString(Explore(documentsRootFolder.ToString());
}
捕获(例外e)
{
HttpContext.Current.Response.Write(e.Message+“
”);
HttpContext.Current.Response.Write(e.StackTrace);
}
}
返回s;
}
私有静态StringBuilder浏览(字符串路径)
{
StringBuilder编写器=新建StringBuilder();
writer.Append(“”);
尝试
{
foreach(System.IO.Directory.GetDirectories(path)中的var a)
{
writer.AppendFormat(“- {0}
”,a.Replace((path.EndsWith(@“\”)?path:path+@“\”,string.Empty));
附加(探索(a));
}
foreach(System.IO.Directory.GetFiles(path)中的var a)
{
字符串url=a.Replace(documentsRootFolder,string.Empty);
string friendlyName=a.Replace((path.EndsWith(@“\”)?path:path+@“\”),string.Empty);
writer.AppendFormat(“”,url,friendlyName);
}
}
捕获{}
writer.Append(“
”);
返回作者;
}
当然,使用表单身份验证,我无法从HttpContext用户处获取windows标识。现在如何读取目录和文件?
注意:我试图使用advapi32.dll获取用户的windows标识。但是,登录后,不关闭浏览器(ssi)就无法注销。这就是为什么我要寻找另一个解决方案。是否可以在不登录的情况下获取windows标识令牌
谢谢可能不安全,也不推荐。但是您可以将用户名和密码保存在会话变量或其他内容中。然后在需要时通过使用模拟
如果由于intranet应用程序中的安全问题而需要自动注销用户,您是否考虑过让IT管理员强制Windows会话自动锁定,因为这可能会实现相同的目标。否,我也希望允许用户在需要时登录(例如,使用另一个用户帐户登录…)在会话变量中保留用户的凭据可能有问题。如果我在会话中存储Impersantion对象,并在用户注销时将其处置,这是否更安全?不,不要这样做。仅当您实际需要模拟时才使用模拟类。
using(new Impersonation("username","domain","password"))
{
s = new MvcHtmlString(Explore(documentsRootFolder).ToString());
}