views:

255

answers:

1

I wish to know the way to add ASP.NET ("Machine_Name"\IIS_IUSRS) to user role in COM+ component programmatically using C#. So whenever my COM+ component is being installed, ASP.NET user is created under Role.

+2  A: 

Here's the code. You've got to reference C:\windows\system32\com\comadmin.dll.

using System;
using COMAdmin;
using Microsoft.VisualBasic;

namespace TesteAdicionaRole
{
    class Program
    {
        static void Main(string[] args)
        {
            string packageName = "TRICOLOR";
            ICOMAdminCatalog catalog = (ICOMAdminCatalog)Interaction.CreateObject("COMAdmin.COMAdminCatalog", string.Empty);
            ICatalogCollection packages = (ICatalogCollection)catalog.GetCollection("Applications");
            packages.Populate();
            foreach (ICatalogObject package in packages)
                if (package.Name.ToString().Equals(packageName))
                {
                    ICatalogCollection roles = (ICatalogCollection)packages.GetCollection("Roles", package.Key);
                    roles.Populate();
                    ICatalogObject role = (ICatalogObject)roles.Add();
                    role.set_Value("Name", "MyRoleName");
                    roles.SaveChanges();
                    ICatalogCollection users = (ICatalogCollection)roles.GetCollection("UsersInRole", role.Key);
                    users.Populate();
                    ICatalogObject user = (ICatalogObject)users.Add();
                    user.set_Value("User", "MV0266\\IUSR_MV0266");
                    users.SaveChanges();
                    break;
                }            
        }
    }
}

[]'s

Fabio Gouw
Thanks! Very detail and useful.
Regina Foo