1

i have got the below code to check whether the login user access to menu item

but if first item fails the if condition it is redirecting to error page, but i need to check second item from that string array with if condition even the first item fails the if condition ...

please find the below given code for test..

var count = 0;
string[] strPageId = PageId.Split(';');


foreach (string menuPageId in strPageId)
{
       if (objDALookup.IsPageVisible(PageType,Int32.Parse(menuPageId), role, AcctId) == false)
       {
           Session["ErrorInfo"] = "Access denied, please contact system administrator.";
           new Lib.Utils.Log().logInfo("FORCE_ACCESS", strUser + " tried to access PageId:" + PageId + " PageType:" + PageType);
           Response.Redirect(ConfigurationManager.AppSettings["ACCESS_DENIED_URL"], true);
       }
       else
       {
            count++;
            if (count == strPageId.Length) break;

       }
 } 
2
  • So do you want to check all items or just next item in the list after the failing one? Commented Jan 28, 2016 at 5:06
  • all items in the same list .. Commented Jan 28, 2016 at 5:08

1 Answer 1

2

You can make condition something like this by figuring out condition in loop and executing the action outside loop once it is decided.

bool access = false;
foreach (string menuPageId in strPageId)
{    
   if (objDALookup.IsPageVisible(PageType,Int32.Parse(menuPageId), role, AcctId) == true)
   {
        access = true; 
        break;
   }
}

if(!access)
{

    Session["ErrorInfo"] = "Access denied, please contact system administrator.";
           new Lib.Utils.Log().logInfo("FORCE_ACCESS", strUser + " tried to access PageId:" + PageId + " PageType:" + PageType);

     Response.Redirect(ConfigurationManager.AppSettings["ACCESS_DENIED_URL"], true);
}

OR

You can use linq

bool access = strPageId.Any(s=> objDALookup.IsPageVisible(PageType,Int32.Parse(menuPageId), role, AcctId) == true);

if(!access)
{

} 
Sign up to request clarification or add additional context in comments.

1 Comment

Putting not with access bool result would do when access is not to any of tab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.