在一個HTTP模塊,承接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
位於同根應用程序。
然而,這將引發ArgumentException
但如果對外部虛擬目錄進行測試。
[ArgumentException:不支持當前應用程序之外的虛擬路徑。 參數名稱:virtualPath]
E.g.
什麼是檢查用戶權限的虛擬目錄中的正確方法是什麼?
您是絕對正確的。我用來確定路徑的方法不正確。 –