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

6 回答 6

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.

于 2012-04-23T20:37:24.567 回答
4
string[] roles = clientContext.GetRoles("").Cast<string>().ToArray();
于 2012-04-23T20:37:37.533 回答
2
using System.Linq;

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

Hope this helps.

于 2012-04-23T20:37:39.970 回答
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.

于 2012-04-23T20:38:03.527 回答
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>";
}
于 2012-04-23T20:43:53.110 回答
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.

于 2012-04-23T20:44:11.870 回答