0

Referring to Azure B2C check user exist or not? I am facing an issue while trying to check if user emailId exists in B2C. Below is the code which I referred to implement from the above mention stackoverflow link where @Saurab Kulkarni suggested the approach, which is close to my requirement, but it always return Status= AwaitingForActivation. Could you please help?

    public interface IGraphHelper
    {
    Task<User> CheckUserByEmailId(string email, CancellationToken ct); 
    }

    public class GraphHelper : IGraphHelper
    {
    private readonly ADOptions _adOptions;

    public async Task<User> CheckUserByEmailId(string email, CancellationToken ct)
    {
    IConfidentialClientApplication confidentialClientApplication = 
                                    ConfidentialClientApplicationBuilder 
                                          .Create(_adOptions.ClientId)     
                                            .WithTenantId(_adOptions.TenantId)
                                            .WithClientSecret(_adOptions.ClientSecret)
                                               .Build();

        ClientCredentialProvider authProvider = new 
                              ClientCredentialProvider(confidentialClientApplication);
    GraphServiceClient graphClient = new GraphServiceClient(authProvider);

    var filter = $"identities/any(c:c/issuerAssignedId eq '{email}' and c/issuer eq '{_adOptions.Issuer}')";

    var request = graphClient.Users.Request()
            .Filter(filter)
               .Select(userSelectQuery)   //What is userSelectQuery here? 
            .Expand(e => e.AppRoleAssignments);

        var userCollectionPage = await request.GetAsync(ct);

        return userCollectionPage.FirstOrDefault();

} }

    //Home Controller//

    public JsonResult CheckName(string emailId, CancellationToken ct)
    { 
        string email = emailId;
        bool status;
        var result = _graphHelper.CheckUserByEmailId(email, ct);
        return Json(status);
    }`

    Error:

=================================

Error Screenshot:

enter image description here

Always returns: Status: Waiting for Activation.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • That kinda looks like a `Task` to me that you are looking at. Maybe you haven't `await`ed the Task? – juunas Feb 05 '21 at 10:53
  • @juunas- Thanks for your response. Here i am awaiting the Task in the above code var userCollectionPage = await request.GetAsync(ct); – Rishi Verma Feb 05 '21 at 10:59
  • Yes but you are not awaiting the function in the controller. – juunas Feb 05 '21 at 11:01
  • 1
    Thanks @juunas for suggestion. I could make it work after making appropriate changes to the way of calling Task and now it works fine. Thanks once again! – Rishi Verma Feb 09 '21 at 13:36
  • Here is some more options to resolve the waitingforactivation error. https://stackoverflow.com/questions/34551008/task-keeps-waiting-for-activation – Jit_MSFT Feb 11 '21 at 06:52
  • @Rishi Did you mean 'public async Task CheckName(string emailId, CancellationToken ct)' sir? – Tiny Wang Feb 26 '21 at 07:34

1 Answers1

0

Tiny and Daniel Mann pointed out the direction, you need to make your event handler async as well:

public async Task<JsonResult> CheckName(string emailId, CancellationToken ct) { 
       
        string email = emailId;
        bool status;
        var result = _graphHelper.CheckUserByEmailId(email, ct);
        return Json(status);
    }
Carl Zhao
  • 8,543
  • 2
  • 11
  • 19