0

我的 .Net Web 应用程序中有一个基页(继承自 System.Web.UI.Page,我的所有页面都继承自该基页),目前如果我使用以下方法

protected void CheckAllowedRole(string UserName, List<string> AllowedRoles)
    {
        try
        {
            bool IsAllowed = false;
            foreach (string item in AllowedRoles)
            {
                if (Roles.IsUserInRole(UserName, item))
                    IsAllowed = true;
            }
            if (!IsAllowed)
                Response.Redirect("~/Members/Error.aspx", false);

        }
        catch (Exception err)
        {
            Response.Redirect("~/Members/Error.aspx", false);
        }
    }

由于某种原因它不知道角色是什么!?!?返回。我什至将用户名传递给这个方法,但仍然不起作用。

但是,如果我将此代码放入从该基本页面继承的页面中效果很好(没问题)。有任何想法吗?对角色(或基类中的成员资格提供者)是否有任何限制。

谢谢

4

1 回答 1

0

instead of providing the user name why don't you try this:

    protected void CheckAllowedRole(List<string> AllowedRoles)
    {
        try
        {
            if (!Page.User.Identity.IsAuthenticated)
                throw new Exception("Unauthenticated User");

            string name = Page.User.Identity.Name;

            bool IsAllowed = false;
            foreach (string item in AllowedRoles)
            {
                IsAllowed = Roles.IsUserInRole(name, item);
            }

            if (!IsAllowed)
                Response.Redirect("~/Members/Error.aspx", false);
        }
        catch (Exception err)
        {
            Response.Redirect("~/Members/Error.aspx", false);
        }
    }
于 2009-06-08T06:34:02.963 回答