Click here to Skip to main content
15,886,067 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to get current background color info from a form Pin
Eddy Vluggen2-Nov-12 5:32
professionalEddy Vluggen2-Nov-12 5:32 
GeneralRe: How to get current background color info from a form Pin
MichCl2-Nov-12 7:53
MichCl2-Nov-12 7:53 
GeneralRe: How to get current background color info from a form Pin
Eddy Vluggen2-Nov-12 8:00
professionalEddy Vluggen2-Nov-12 8:00 
GeneralRe: How to get current background color info from a form Pin
MichCl2-Nov-12 8:06
MichCl2-Nov-12 8:06 
QuestionRe: How to get current background color info from a form Pin
Eddy Vluggen2-Nov-12 9:09
professionalEddy Vluggen2-Nov-12 9:09 
GeneralRe: How to get current background color info from a form Pin
Alan N2-Nov-12 10:00
Alan N2-Nov-12 10:00 
AnswerRe: How to get current background color info from a form Pin
Ravi Bhavnani2-Nov-12 9:34
professionalRavi Bhavnani2-Nov-12 9:34 
QuestionActive Directory UserPrincipal Extension not working Pin
rcgneo1-Nov-12 22:44
rcgneo1-Nov-12 22:44 
Hello,

I have added an Extended attribute (extensionAttribute1) to represent a special app ID (string) in Active Directory.

Now, I have extended the UserPrincipal class so I can see this new attribute but I get exceptions when running the code.

Any help to figure out what is wrong will be much appreciated.

Exceptions:

2012-11-02 03:14:43,659 [10] DEBUG Classes.Utils.MyHelper - DoesUserExist: Exception while trying to query Active Directory. at System.DirectoryServices.AccountManagement.Principal.MakePrincipal(PrincipalContext ctx, Type principalType)
at System.DirectoryServices.AccountManagement.SDSUtils.SearchResultToPrincipal(SearchResult sr, PrincipalContext owningContext, Type principalType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at
Classes.Utils.UserPrincipalExtension.FindByIdentity(PrincipalContext context, String identityValue) in C:\visual studio 2010\Projects\App\Classes\Utils\UserPrincipalExtension.cs:line 61
at App.Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 69
2012-11-02 03:14:43,659 [10] DEBUG App._Default - UserID_Changed: Exception trying to lookup user in Active Directory. at Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 94
at App._Default.UserID_Changed(Object sender, EventArgs e) in C:\visual studio 2010\Projects\App\Default.aspx.cs:line 105


Extension Class:

C#
[DirectoryObjectClass("person")]
[DirectoryRdnPrefix("CN")]

public class UserPrincipalExtension : UserPrincipal
{
    // Implement the constructor using the base class constructor.
    public UserPrincipalExtension(PrincipalContext context, string userName) : base(context) { }

    // Implement the constructor with initialization parameters.
    public UserPrincipalExtension(PrincipalContext context,
                     string samAccountName,
                     string password,
                     bool enabled)
        : base(context,
               samAccountName,
               password,
               enabled)
    {
    }

    UserPrincipalExSearchFilter searchFilter;

    new public UserPrincipalExSearchFilter AdvancedSearchFilter
    {
        get
        {
            if (null == searchFilter)
                searchFilter = new UserPrincipalExSearchFilter(this);

            return searchFilter;
        }
    }

    [DirectoryProperty("extensionAttribute1")]
    public string extensionAttribute1
    {
        get
        {
            if (ExtensionGet("extensionAttribute1").Length != 1)
                return null;

            return (string)ExtensionGet("extensionAttribute1")[0];

        }
        set { this.ExtensionSet("extensionAttribute1", value); }
    }

    // Implement the overloaded search method FindByIdentity
    public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
                                                   string identityValue)
    {
        return (UserPrincipalExtension)FindByIdentityWithType(context,
                                                     typeof(UserPrincipalExtension),
                                                     identityValue);
    }

    // Implement the overloaded search method FindByIdentity
    public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
                                                   IdentityType identityType,
                                                   string identityValue)
    {
        return (UserPrincipalExtension)FindByIdentityWithType(context,
                                                     typeof(UserPrincipalExtension),
                                                     identityType,
                                                     identityValue);
    }
}

public class UserPrincipalExSearchFilter : AdvancedFilters
{
    public UserPrincipalExSearchFilter(Principal p) : base(p) { }
}


Application Call:

C#
public static bool DoesUserExist(string usr)
{
    string methodName = "DoesUserExist: ";
    bool result = false;
    PrincipalContext context = null;
    UserPrincipalExtension user = null;

    try
    {
        context = new PrincipalContext(ContextType.Domain, "MYDOM.LOCAL");


        user = UserPrincipalExtension.FindByIdentity(context, usr);
        if ((user != null) && (user.extensionAttribute1 != null))
        {
            // user id present in AD
            log(methodName, "User does exist. UserID=" + usr + "extension=" + user.extensionAttribute1);
            result = true;
        }
        else
        {
            log(methodName, "User does NOT exist.");
            // User does not exist in AD
        }

    }
    catch (Exception ex)
    {
        log(methodName, "Exception while trying to query Active Directory. " + ex.StackTrace);
        throw ex;
    }
    finally
    {

        // dispose of objects
        user.Dispose();
        context.Dispose();
    }

    return result;
}

AnswerRe: Active Directory UserPrincipal Extension not working Pin
Eddy Vluggen2-Nov-12 0:50
professionalEddy Vluggen2-Nov-12 0:50 
GeneralRe: Active Directory UserPrincipal Extension not working Pin
rcgneo2-Nov-12 5:48
rcgneo2-Nov-12 5:48 
GeneralRe: Active Directory UserPrincipal Extension not working Pin
Eddy Vluggen2-Nov-12 6:56
professionalEddy Vluggen2-Nov-12 6:56 
GeneralRe: Active Directory UserPrincipal Extension not working Pin
rcgneo2-Nov-12 9:35
rcgneo2-Nov-12 9:35 
GeneralRe: Active Directory UserPrincipal Extension not working Pin
Eddy Vluggen2-Nov-12 9:57
professionalEddy Vluggen2-Nov-12 9:57 
QuestionProtected Interface Members Pin
Ambika Jadhav1-Nov-12 20:22
Ambika Jadhav1-Nov-12 20:22 
AnswerRe: Protected Interface Members Pin
Richard MacCutchan2-Nov-12 0:46
mveRichard MacCutchan2-Nov-12 0:46 
AnswerRe: Protected Interface Members Pin
Abhinav S2-Nov-12 0:49
Abhinav S2-Nov-12 0:49 
AnswerRe: Protected Interface Members Pin
BobJanova2-Nov-12 0:55
BobJanova2-Nov-12 0:55 
AnswerRe: Protected Interface Members Pin
markovl2-Nov-12 1:44
markovl2-Nov-12 1:44 
GeneralRe: Protected Interface Members Pin
Ambika Jadhav4-Nov-12 16:09
Ambika Jadhav4-Nov-12 16:09 
QuestionWhy does this code hang? (async) Pin
SledgeHammer011-Nov-12 19:07
SledgeHammer011-Nov-12 19:07 
AnswerRe: Why does this code hang? (async) Pin
OriginalGriff1-Nov-12 22:24
mveOriginalGriff1-Nov-12 22:24 
GeneralRe: Why does this code hang? (async) Pin
SledgeHammer012-Nov-12 6:40
SledgeHammer012-Nov-12 6:40 
AnswerRe: Why does this code hang? (async) Pin
Eddy Vluggen2-Nov-12 2:00
professionalEddy Vluggen2-Nov-12 2:00 
QuestionWCF Windows Service with ASP.net Application authentication problem... Pin
JD861-Nov-12 11:37
JD861-Nov-12 11:37 
AnswerRe: WCF Windows Service with ASP.net Application authentication problem... Pin
JD861-Nov-12 11:48
JD861-Nov-12 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.