views:

205

answers:

2

Hi there

I have a base page (inherited from System.Web.UI.Page and all my pages inherit from this base page) in my .Net web application and at the moment if I put the following methods

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);
        }
    }

for somereason it doesn't know the role is!?!? Return. I even pass the username into this methods and still doesn't work either.

But if I take this code and put into my page which inherited from this base page works well (no issue). Any ideas? Is there any restriction on the Roles (or Membership provider in the base class).

Thanks

A: 

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);
        }
    }
TheVillageIdiot
A: 

Never mind on this. I am on drug. It is working nicely.

dewacorp.alliances

related questions