gpt4 book ai didi

c# - 如何测试虚拟目录的用户权限?

转载 作者:太空狗 更新时间:2023-10-29 17:58:13 25 4
gpt4 key购买 nike

在 HttpModule 中,在 url 重写之后,我正在测试用户对位于我的应用程序中的虚拟路径的权限,使用:

// Since we are now rewriting the path we need to check again that the 
// current user has access to the rewritten path.
// Get the user for the current request
// If the user is anonymous or authentication doesn't work for this suffix
// avoid a NullReferenceException in the UrlAuthorizationModule by creating
// a generic identity.
string virtualCachedPath = cache.GetVirtualCachedPath();

IPrincipal user = context.User ?? new GenericPrincipal(
new GenericIdentity(string.Empty, string.Empty), new string[0]);

// Do we have permission to call
// UrlAuthorizationModule.CheckUrlAccessForPrincipal?
PermissionSet permission = new PermissionSet(PermissionState.None);
permission.AddPermission(
new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted));
bool hasPermission =
permission.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);
bool isAllowed = true;

// Run the rewritten path past the auth system again, using the result as
// the default "AllowAccess" value
if (hasPermission && !context.SkipAuthorization)
{
isAllowed = UrlAuthorizationModule.CheckUrlAccessForPrincipal(
virtualCachedPath, user, "GET");
}

virtualCachedPath 是任何虚拟路径,例如位于应用程序根目录的 ~/app_data/cache

http://msdn.microsoft.com/en-us/library/system.web.security.urlauthorizationmodule.checkurlaccessforprincipal(v=vs.110).aspx

然而,如果针对外部虚拟目录进行测试,则会抛出 ArgumentException

[ArgumentException: Virtual path outside of the current application is not supported. Parameter name: virtualPath]

例如

Example virtual directory in IIS

检查用户对虚拟目录的权限的正确方法是什么?

最佳答案

当传递给CheckUrlAccessForPrincipal 是相对的 URL 格式路径(“~/PATH”)。相反,如果我使用文件系统约定(“C:\PATH\”)传递物理路径,我会得到您描述的 ArgumentException

所以我怀疑 virtualCachedPath 实际上可能是一个文件系统格式的路径,至少在引发异常的情况下是这样。我建议您在应用程序中实现日志记录,以便在引发异常时可以仔细检查 virtualCachedPath 的值:

try
{
isAllowed = UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualCachedPath, user, "GET");
}
catch (ArgumentException ex)
{
Trace.TraceInformation("VirtualCachedPath: {0}", virtualCachedPath);
throw;
}

关于c# - 如何测试虚拟目录的用户权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24884949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com