I'm using C# in VS2010 and I need some help with a web application. I don't have much experience with web services. I was given the url to a webservice containing methods required to build the login part of the application. No documentation. I have the login piece working though. Then I get stuck. Upon successful login I need to call another method which returns a list (or object?) of Roles that the authenticated user has access to. The items it returns for myself for example are (name, nameField) and a total of 20 roles. I just want to see if 1 role exists out of the 20.

protected void Button_Click(object sender, EventArgs e)
    	{
    		ServiceReference1.Identity usr = new ServiceReference1.Identity();
    		loginService.AuthenticationService auth = new loginService.AuthenticationService();
    		loginService.AuthenticationService auth = new loginService.AuthenticationService();
    		auth.Login(TextBox1.Text, TextBox2.Text, "10.55.31.91");
    		List<object> roles = new List<object>(auth.GetIdentityRoles(TextBox1.Text));
    		IEnumerable myEnum = roles;
    		IEnumerator myEnumerator = myEnum.GetEnumerator(); //Getting the Enumerator
    		myEnumerator.Reset(); //Position at the Beginning
    		while (myEnumerator.MoveNext()) //Till not finished do print
    		{
    			Response.Write(myEnumerator.Current.ToString());
    		}
    	}

Now, if I hover over "roles" in line 6 while debugging I can see the field I want to search. I want to know if "Name" contains "Administrator" but all my examples only return "loginService.Role" in line 13. It just writes loginService.Roles 20 times. I need to get down to the next level. It's Friday and it's my Birthday, please help me out lol.

[+] roles = Count = 20
[+] {loginService.Role}
Name = "Administrator"
nameField = "Administrator"

Well seeing as you are interpretting all the data as generic objects, it doesn't suprise me that .ToString() of each item has a generic value. Instead of using objects, try to figuire out exactly what datatype this is. Try using intellisence on the GetIdentityRoles to see what it's actually returning as a datatype.

Assuming they are of type loginService.Role:

var myRoles = auth.GetIdentityRoles(TextBox1.Text));

bool isAdmin = false;
foreach (loginService.Role x in myRoles)
   if (x.Name == "Administrator")
   {
      isAdmin = true;
      break;
   }

Using LINQ you don't really have to worry about the datatype (assuming it was loaded as the proper type):

var myRoles = auth.GetIdentityRoles(TextBox1.Text));

//To just determine if theres an admin role
bool IsAdmin = (from x in myRoles
                where x.Name=="Administrator"
                select x)
                .Count != 0;

//or to get a list of all the roles to keep in memory...
var AdminRoleCollection = (from x in myRoles
                where x.Name=="Administrator"
                select x);
bool IsAdmin = AdminRoleCollection().Count != 0;

//Another method that loads the .Names of the roles to a collection of strings then determines if it contains "Administrator"
bool IsAdmin = (from x in myRoles
                select x.Name)
               .Contains("Administrator");

//And one more method using an annonymous method for good measure
bool IsAdmin = myRoles.Count(x => x.Name == "Administrator") != 0;
commented: Thanks skatmatic! +1
var myRoles = auth.GetIdentityRoles(TextBox1.Text));

bool isAdmin = false;
foreach (loginService.Role x in myRoles)
   if (x.Name == "Administrator")
   {
      isAdmin = true;
      break;
   }

This is what I needed to access the Name value. But I would like to understand more of what you mean by object vs type. The wsdl says its an object that returns a string. Which is why I was adding .ToString()

<s:element name="GetIdentityRoles">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="identityName" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>

This is what I needed to access the Name value. But I would like to understand more of what you mean by object vs type. The wsdl says its an object that returns a string. Which is why I was adding .ToString()

<s:element name="GetIdentityRoles">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="identityName" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>

I'm not overly familiar with wsdl, but it looks like that is saying the element GetIdentityRoles returns an object with a property of type string called identityName. Since it only contains one property, it would probably be safe to inherit that class with a wrapper class and override .ToString() to return the identityName. But that means doing a bit of work for almost no reason.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.