Issue when check identity FindByNameAsync on debug it hang and stuck and not complete ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-03-01T12:20:56.7066667+00:00

I work on .net core 7 blazor web app . i face issue when working on identity user register .

when debug on function userManager.FindByNameAsync it hang and stuck

so I try to add watch to function userManager.FindByNameAsync to check reason

I get message function evaluation require all thread to run when make add

so How to solve this issue please And are that will make hang on run time application run on IIS .

my code generate error

public async Task<ResponseModel> Submit(UserDto request)
        {
          
                string roleName = request.UserRole == 1 ? "Normal" : "Admin";
                request.EmailAddress = "xyza3211116@gmail.com";
               
                var Identityuser = await userManager.FindByNameAsync(request.UserName);
                var newUserIdentity = new ApplicationUser()
                {
                    FullName = request.UserName,
                    Email = request.EmailAddress,
                    UserName = request.UserName
                };
                var newUserResponse = await userManager.CreateAsync(newUserIdentity, "Cxx90111@7777?");
            }

when debug it hangs on two variables Identityuser and newUserResponse

and give me function evaluation require all thread to run When make add watch to these two function userManager.CreateAsync and userManager.FindByNameAsync

so How to ignore this message and resume debugging AND see my result without hang

from debug

newUserResponse

variable

Id = 1127, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

I using user manager for microsoft

 private readonly UserManager<IdentityUser> userManager;
        private readonly RoleManager<IdentityRole> roleManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
name space used

using Microsoft.AspNetCore.Identity;
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,622 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,512 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,029 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Washington 911 Reputation points MVP
    2023-03-01T14:54:28.35+00:00

    Try changing this line to:

    var Identityuser = userManager.FindByNameAsync(request.UserName).Result;

    0 comments No comments

  2. Ahmed Salah Abed Elaziz 390 Reputation points
    2023-03-01T18:33:42.0666667+00:00

    thank you for support

    this is solved

    var Identityuser = userManager.FindByNameAsync(request.UserName).Result; --solved

    but still can't add role

    userManager.AddToRoleAsync(newUserIdentity, roleName).Result; --still yet not solving

    issue on bold line on function submit

    it give me same issue function evaluation require all thread to run

    full code details

    public class ResponseModel
    {
    
        public object result { get; set; }
    
        public string message { get; set; }
    
        public Status status { get; set; }
    
        public void Success(object data)
    
        {
    
            result = data;
    
            message = "success";
    
            status = Status.success;
    
        }
    
        public void Success()
    
        {
    
            result = null;
    
            message = "success";
    
            status = Status.success;
    
        }
    
        public void Failed(string errorMsg)
    
        {
    
            result = null;
    
            message = errorMsg;
    
            status = Status.InternalServerError;
    
        }
    
        public void PartiallySucceed(string errorMsg)
    
        {
    
            result = null;
    
            message = "partially Succeed : " + errorMsg;
    
            status = Status.success;
    
        }
    
    }
    
    public enum Status
    
    {
    
        success = 200,
    
        SuccessButDataNoFound = 404,
    
        InternalServerError = 500,
    
        Unauthorized = 401,
    
        BadRequest = 404
    
    }
    
        public async Task<ResponseModel> Submit(UserDto request)
    {
    
            try
    
            {
    
                string roleName = request.UserRole == 1 ? "Normal" : "Admin";
    
                request.EmailAddress = "Mchelaziz.ga.ba@gmail.com";
    
                var Identityuser =   userManager.FindByNameAsync(request.UserName).Result;
    
                var newUserIdentity = new IdentityUser()
    
                {
    
                    UserName = request.UserName
    
                };
    
                var newUserResponse =  userManager.CreateAsync(newUserIdentity, "Cxyz90191@1234?").Result;
    
                //if (newUserResponse.Succeeded)
    
    
                    
    
                    **var a=    userManager.AddToRoleAsync(newUserIdentity, roleName).Result;//issue here** 
    
                if (Identityuser == null)
    
                {
    
                    //First Check If User Exist
    
                    var UserExsit = _UsersRepository.GetList(x => x.UserName == request.UserName).FirstOrDefault();
    
                    if (UserExsit == null)
    
                    {
    
                        //check the User is on Active Directory
    
                        string adPath = "LDAP://DC=XXX,DC=XX";
    
                        var adAuth = new LdapAuthentication(adPath).userexists(request.UserName, "xx");
    
                        if (adAuth)
    
                        {
    
                            var newUser = new Users();
    
                            newUser.UserName = request.UserName;
    
                            newUser.IsActive = true;
    
                            newUser.UserRoll = request.UserRole;
    
                            newUser.UserType = request.UserType;
    
                            newUser.CreatedBy = 1;
    
                            newUser.CreatedDate = DateTime.Now;
    
                            var result = _UsersRepository.Insert(newUser);
    
                            if (result.ID != 0)
    
                            {
    
    
                                
    
                                IdentityRole role_ = new IdentityRole(roleName);
    
                                var exists = await roleManager.RoleExistsAsync(roleName);
    
                                if (!exists)
    
                                {
    
                                    var results = await roleManager.CreateAsync(role_);
    
                                    if (results.Succeeded)
    
                                    {
    
                                        var user_ = await userManager.CreateAsync(new IdentityUser { UserName = result.UserName, NormalizedUserName = result.UserName });
    
                                        var user = await userManager.FindByNameAsync(request.UserName);
    
                                        await userManager.AddToRoleAsync(user, roleName);
    
                                    }
    
                                }
    
                                else
    
                                {
    
                                    var user_ = await userManager.CreateAsync(new IdentityUser { UserName = result.UserName, NormalizedUserName = result.UserName });
    
                                    var user = await userManager.FindByNameAsync(request.UserName);
    
                                    await userManager.AddToRoleAsync(user, roleName);
    
                                }
    
                                _response.Success(newUser);
    
                            }
    
                            else
    
                                _response.Failed("Sorry, Failed to Registeration User");
    
                        }
    
                        else
    
                            _response.Failed("Sorry, The User Not Exist in Active Directory");
    
                    }
    
                    else
    
                        _response.Failed("Sorry, The User Is Already Exist");
    
                }
    
                else
    
                {
    
                    _response.Failed("Sorry, The User Is Already Exist");
    
                }
    
    
               
    
            }
    
            catch(Exception ex)
    
            {
    
                _response.Failed("Failed to save User");
    
            }
    
            return _response;
    
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.