In my C# application I am trying to search for a User via Graph API. The only parameter I have is the username which is stored in onPremisesSamAccountName field.
Through Graph Explorer I can successfully run the query
https://graph.microsoft.com/v1.0/users?$count=true&$search="onPremisesSamAccountName:myusername"&$select=id,displayName
And Graph Explorer gives me the C# code to use
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var users = await graphClient.Users
.Request()
.Search("onPremisesSamAccountName:myusername")
.Select("id,displayName")
.GetAsync();
Now when I try to use that code I get an error saying that Search is not a method, do I need to add an extra package to use Search?
I also didn't find any nuget package with Search method.
You can specify search value by using query option. $search query parameter requires a request header ConsistencyLevel: eventual
var queryOptions = new List<Option>()
{
new QueryOption("$search", "\"onPremisesSamAccountName:myusername\""),
new HeaderOption("ConsistencyLevel", "eventual")
};
var users = await graphClient.Users
.Request(queryOptions)
.Select("id,displayName")
.GetAsync();
Related
Tried this:
graphServiceClient
.sites(siteId)
.pages(pageId)
.buildRequest()
.get() but seems earlier pages method was supported and now it's not appearing.
Is there any way to get site page collection?
thanks for reaching out ,I understood that you are trying to get list of sites, looks like you are using async await ,please convert your get() function to GetAync() and add await.
Could you please try by using the below sample code
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var items = await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items .Request()
.GetAsync();
ref docs - https://learn.microsoft.com/en-us/graph/api/listitem-list?view=graph-rest-1.0&tabs=csharp#request
Hope this helps , please let us know if you have any query
thanks
I have this code below, i need to login but Jira documantation so bad that i cant figured out, how can i login to jira ? They say basic auth is depreced but all examples are like this. How can i login ?
var apiUrl = #"https://MYDOMAIN.atlassian.net/rest/api/3/issue/MYPROJECTNAME";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), apiUrl))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("xxxxx#gmail.com:1xxxxxk2qdnqHJxxxx155"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
var response = await httpClient.SendAsync(request);
}
}
You are right, documentation too weak.
In my use cases, I preferred using "username" only. Not email. Can you try this?
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("<YOUR_USERNAME>:1xzyNAGgksdfgP3155"));
This is Jira documentation:
The username and password of a user who has permission to create issues on your Jira Server site
Build a string of the form username:password.
If this recommendation fails can you share your rest logs?
I've been trying to build a query to return all SPO sites in a tenant using MSGraph. I can do this in the Graph Explorer with the following query:
GET https://graph.microsoft.com/v1.0/sites?search=*
This MSdocs article seems to suggest that it's possible to use the search parameter but the C# example does not use search.
Has anyone been able to return all SPO sites in a tenant using the .Net SDK?
I'm using the following code to search for a site.
Add search as a query option in a request.
IGraphServiceClient client;
private async Task<Site> FindSiteAsync(string value)
{
var siteQueryOptions = new List<QueryOption>()
{
new QueryOption("search", value)
};
var sites = await client.Sites.Request(siteQueryOptions).GetAsync();
var site = sites.FirstOrDefault();
return site;
}
Your c# example for https://graph.microsoft.com/v1.0/me/people?$search="jesper"
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var people = await graphClient.Me.People
.Request()
.Search("jesper")
.GetAsync();
Error I'm getting:
not sure if this is meant to happen but seems wrong that you cant use the search function
https://developer.microsoft.com/en-us/graph/graph-explorer
Yes, you're absolutely right. Those snippets are generated by a tool, and sometimes it gets it wrong. Thanks for reporting!
You can add any query parameters to requests sent by the SDK though, so you can still make this work like so:
var options = new List<QueryOption> { new QueryOption("$search", "j") };
var people = await graphClient.Me.People.Request(options).GetAsync();
Trying to access discovery client for acceising other endpoints anf following with,
http://docs.identityserver.io/en/aspnetcore1/endpoints/discovery.html
Installed IdentityModel nuget package in .Net 7.5 MVC application. But unable to find the DiscoveryClient.
var discoveryClient = new DiscoveryClient("https://demo.identityserver.io");
var doc = await discoveryClient.GetAsync();
Is there something change in Identitymodel for IdentityServer4
Also, unable to find parameter for "Tokenclient".
Able to figure out, change in IdentityModel, its all extension of HttpClient.
https://identitymodel.readthedocs.io/en/latest/client/discovery.html
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://demo.identityserver.io");
Yes, you are correct. There are lot of changes in the IdentityModel NuGet package.
Below code will help you:
HttpClient httpClient = new HttpClient();
//Below code will give you discovery document response previously we were creating using DiscoveryClient()
// They have created `.GetDiscoveryDocumentAsync()` extension method to get discovery document.
DiscoveryDocumentResponse discoveryDocument = await httpClient.GetDiscoveryDocumentAsync();
// To create a token you can use one of the following methods, which totally depends upon which grant type you are using for token generation.
Task<TokenResponse> RequestAuthorizationCodeTokenAsync(AuthorizationCodeTokenRequest)
Task<TokenResponse> RequestClientCredentialsTokenAsync(ClientCredentialsTokenRequest)
Task<TokenResponse> RequestDeviceTokenAsync(DeviceTokenRequest)
Task<TokenResponse> RequestPasswordTokenAsync(PasswordTokenRequest)
Task<TokenResponse> RequestRefreshTokenAsync(RefreshTokenRequest)
Task<TokenResponse> RequestTokenAsync(TokenRequest)
For example if you want to create a token for password grant type then use below code:
PasswordTokenRequest passwordTokenRequest = new PasswordTokenRequest()
{
Address = discoveryDocument.TokenEndpoint,
ClientId = ClientName,
ClientSecret = ClientSecret,
GrantType = GrantTypes.ResourceOwnerPassword,
Scope = scope,
UserName = userName,
Password = password
};
httpClient.RequestPasswordTokenAsync(passwordTokenRequest);
I hope this will help you!
If you used some sample code and the other answers aren't working, because HttpClient doesn't have GetDiscoveryDocumentAsync
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://localhost:5001");
Update your IdentityModel package, in Visual Studio:
Right click Dependencies -> Manage Nuget Packages -> Updates (select "All" in top right corner)