0

Have you got another idea to get this list of data?

I would like to show all my code because I think this can be much more easier for you. In this code you can see how I connect with role file.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Interop.Security.AzRoles;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Collections.Generic;




namespace Authman
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            // See the following table for other sample connection strings.
            string connectionString = @"msxml://c:\plik.xml";

            AzAuthorizationStoreClass azStore = new AzAuthorizationStoreClass();
            azStore.Initialize(0, connectionString, null);

            IAzApplication2 azApplication = azStore.OpenApplication2("SatheeshApp", null);

            IAzClientContext3 clientContext = (IAzClientContext3)azApplication.InitializeClientContextFromToken((ulong)WindowsIdentity.GetCurrent().Token, null);


            // Use the default application scope.
            string[] roles = (string[])clientContext.GetRoles("");


            foreach (string role in roles)
            {
                Span3.InnerHtml += role.ToString() + "</br>";
            }



        }
    }
}

enter image description here

4
  • 5
    What is clientContext declared as? Commented Apr 23, 2012 at 20:36
  • You can try clientContext.GetRoles("").Select(r => r.ToString()).ToArray(); , although i have also no idea what clientContext.gGetRoles returns. Commented Apr 23, 2012 at 20:38
  • What is the return type of GetRoles? Looks like it's returning an Object[] in this message, while in your errors listed below it returns Object...? Commented Apr 23, 2012 at 20:52
  • @Tim Schmelter Error Error 1 'object' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 36 30 Authman Commented Apr 23, 2012 at 21:19

6 Answers 6

4
string[] roles = clientContext.GetRoles("").Cast<string>().ToArray();
Sign up to request clarification or add additional context in comments.

5 Comments

Errors:Error 1 'object' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 36 30 Authman
Are you sure clientContext.GetRoles("") is returning an array of Object and not just a single Object?
I`m sure you have got this in picture. Application import Roles from Active Directory
I have the same question as @SteveDanner, look this: dl.dropbox.com/u/15208254/stackoverflow/objectarray.jpg
I agree with @OscarJara, it seems like something isn't right with the GetRoles call. Either his answer or mine should work just fine.
4

Use the Cast<string>() method. This is related to covariance/contravariance. You need to cast each item in the array (which is what the Cast method does) rather than the entire array. If you really need to convert it into another string array you can use the ToArray method, but since here you're just using a foreach on it there is no need.

foreach(string role in clientContext.GetRoles("").Cast<string>())
{
  //use role
}

You'll need to add a using System.Linq to get the Cast extension method.

8 Comments

Errors Error 1 'object' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 35 37 Authman
Error 2 Instance argument: cannot convert from 'object' to 'System.Data.EnumerableRowCollection' C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 35 37 Authman
So what do you think you should do? What is that error message saying to you?
I will be honest with you I'm sitting with this problem a lot of time but I will be honest with you I have not idea where is problem.
What does the method GetRoles return, what type?
|
2
using System.Linq;

string[] roles = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();

Hope this helps.

2 Comments

Errors: string[] roles = clientContext.GetRoles("").OfType<object>().Select(o => o.ToString()).ToArray(); Error:Error 2 'object' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 38 57 Authman
look this, in this example I convert int to string from object array: dl.dropbox.com/u/15208254/stackoverflow/objectarray.jpg Maybe your clientContext.GetRoles("") is not well declared.
2
object[] roles=clientContext.GetRoles("");
foreach(object role in roles)
{
   Span3.InnerHtml + = role.ToString() + "</br>";
}

Why you need to cast your array to string[]? You can use it without casting at all.

1 Comment

Errors: Error 1 Cannot implicitly convert type 'object' to 'object[]'. An explicit conversion exists (are you missing a cast?) C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 38 30 Authman
1

You can just cast to the object[] the error indicates it to be instead;

object[] roles = (object[])clientContext.GetRoles("");

foreach(object role in roles)
{
    Span3.InnerHtml += role.ToString() + "</br>";
}

3 Comments

Error Error 1 Cannot implicitly convert type 'object' to 'object[]'. An explicit conversion exists (are you missing a cast?) C:\Users\Rafał\Documents\Visual Studio 2010\Projects\Authman\Authman\WebForm1.aspx.cs 38 30 Authman
@RafalPolandBeginner Seems the return type is object, but it actually contains an object[], try my edit above.
thx for help your answer was correct now application showing me all roles for my user -Menager - Finance Menager - HR Menager :) from active directory
1

As others have pointed out, clientContext.GetRoles("").Cast<string>().ToArray() is the work around.

Lets say what happens if the conversion is legal. you have a List listOfAnimals.

List<Cow> listOfCows = GetListOfCows();

listOfAnimals = listOfCows;

//After some lines of code

List<Tiger> listOfTiger = GetListOfTigers();

listOfAnimals.Add(listOfTiger); //Epic fail.

You might have accidentally added Tiger to a list of Cow. Which is wrong and dangerous. The CLR does not allow you to introduce such bugs in to the code.

Comments

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.