Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.DirectoryServices.AccountManagement Version 9.0: UserPrincipal.GetAuthorizationGroups() throws System.UriFormatException: Invalid URI: The hostname could not be parsed. #109858

Open
uwer62 opened this issue Nov 15, 2024 · 8 comments · May be fixed by #109977
Assignees
Labels
area-System.DirectoryServices bug in-pr There is an active PR which will close this issue when it is merged regression-from-last-release
Milestone

Comments

@uwer62
Copy link

uwer62 commented Nov 15, 2024

Description

After updating from 8.0.1 to 9.0.0 the call above throws the following exception:
System.UriFormatException: Invalid URI: The hostname could not be parsed.

Reproduction Steps

var ctx = new PrincipalContext(ContextType.Domain);
var usersearcher = new PrincipalSearcher(new UserPrincipal(ctx));
var users = usersearcher.FindAll().Cast<UserPrincipal>();
var user = users.FirstOrDefault(e => e.SamAccountName == "valid user name");
var groupList = user?.GetAuthorizationGroups().ToList(); <== throws in 9.0.0

Expected behavior

This call should return the groups as the older versions

Actual behavior

Throws unexpected exception

Regression?

Yes. worked in 8.0.1

Known Workarounds

downgrade to 8.0.1

Configuration

.NET 8.0 and .NET 9.0 on Windows 11

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 15, 2024
@jasasa
Copy link

jasasa commented Nov 17, 2024

I have the same issue with GetGroups(). Works in 8.0, not in 9.0.
If I include the name of the domain in the PrincipalContext-call, it works:
var ctx = new PrincipalContext(ContextType.Domain, "domainname");

@ericstj
Copy link
Member

ericstj commented Nov 17, 2024

No repro for me with the given code sample I get the same results from both package versions. Could you please share the full details of the exception and callstack? Could you confirm what runtime are you using - .NET 8.0 or .NET 9.0? Thanks.

@ericstj ericstj added the needs-author-action An issue or pull request that requires more info or actions from the author. label Nov 17, 2024
@uwer62
Copy link
Author

uwer62 commented Nov 17, 2024

I have created a new console application (.NET 9.0) and added the code I mentioned to Program.cs.
Here is the full code:

// See https://aka.ms/new-console-template for more information
using System.DirectoryServices.AccountManagement;

Console.WriteLine("Hello, World!");
var ctx = new PrincipalContext(ContextType.Domain);
var usersearcher = new PrincipalSearcher(new UserPrincipal(ctx));
var users = usersearcher.FindAll().Cast<UserPrincipal>();
var user = users.FirstOrDefault(e => e.SamAccountName == "Here I used a valid user name in our domain");
var groupList = user?.GetAuthorizationGroups().ToList();
Console.WriteLine(groupList.Count.ToString());

Then I added System.DirectoryServices.AccountManagetment 9.0.0 and ran the code
The exception is thrown in the following line:
var user = users.FirstOrDefault(e => e.SamAccountName == "Here I used a valid user name in our domain");

HResult: -2146233033
Message: Invalid URI: The hostname could not be parsed.
Source: System.Private.Uri
Stack Trace:
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at System.Uri..ctor(String uriString)
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName()
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p)
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper()
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups()
at Program.

$(String[] args) in C:\Users\ur\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 9

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Nov 17, 2024
@uwer62
Copy link
Author

uwer62 commented Nov 17, 2024

I can confirm @jasasa's comment that the problem goes away if I add the name of our domain to the following line:
var ctx = new PrincipalContext(ContextType.Domain, "our domain name");

@steveharter
Copy link
Member

Appears to be introduced by https://github.com/dotnet/runtime/pull/89787/files#diff-36ef1673850535f3dd5645a5b990a17652b2eaff381d3abaed74fc29278665afR2421

@steveharter steveharter added bug regression-from-last-release and removed untriaged New issue has not been triaged by the area owner labels Nov 18, 2024
@steveharter steveharter added this to the 10.0.0 milestone Nov 18, 2024
@steveharter steveharter self-assigned this Nov 18, 2024
@steveharter
Copy link
Member

@kumarravik78c is it possible you can provide a fix for this? Thanks

@steveharter
Copy link
Member

If I include the name of the domain in the PrincipalContext-call, it works:
var ctx = new PrincipalContext(ContextType.Domain, "domainname");

@jasasa @uwer62 do you have examples of what was throwing?

I verified that this throws:
LDAP://CN=John Doe,CN=Users
while this does not:
LDAP://myserver.local:12345/CN=John Doe, CN=Users

@uwer62
Copy link
Author

uwer62 commented Nov 19, 2024

@steveharter Currently I only have the example and stack trace I had posted above. Are you looking for something specific?

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.DirectoryServices bug in-pr There is an active PR which will close this issue when it is merged regression-from-last-release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants