Home > Mobile >  Http failure during parsing for https
Http failure during parsing for https

Time:11-20

I have used Angular 14 in frontend and .NET 7 in backend. When I refresh the page I get this error:

Http failure during parsing for https://localhost:6001/Account/Login?ReturnUrl=/api/account/currentuser"

How can I solve this problem?

in backend:

I think that the cause of the problem is in this line of code:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>()
                     .AddRoles<ApplicationRole>()
                     .AddEntityFrameworkStores<ShopDbContext>();

because if I remove

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>() .AddRoles() .AddEntityFrameworkStores();

and write this instead of it

builder.AddEntityFrameworkStores();

and remove this part of code from GetCurrentUser()

var role = (await _userManager.GetRolesAsync(user)).FirstOrDefault(); if (role == null) { role = Role.Standard.ToString("f"); await _userManager.AddToRoleAsync(user, role); }

The app works but without role functionality.

AddIdentityServices method:

 public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
        {
            var builder = services.AddIdentityCore<ApplicationUser>();

            builder = new IdentityBuilder(builder.UserType, builder.Services);
            builder.AddSignInManager<SignInManager<ApplicationUser>>();
 // I think that the cause of the problem is in this line of code.
            builder.Services.AddIdentity<ApplicationUser, ApplicationRole>()
                     .AddRoles<ApplicationRole>()
                     .AddEntityFrameworkStores<ShopDbContext>();
            
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Token:Key"])),
                    ValidIssuer = config["Token:Issuer"],
                    ValidateIssuer = true,
                    ValidateAudience = false
                };
            });

            return services;
        }

GetCurrentUser method:

        [Authorize]
        [HttpGet]
        public async Task<ActionResult<UserDto>> GetCurrentUser()
        {
            var user = await _userManager.FindByEmailFromClaimsPrinciple(User);
            if (user == null)
            {
                return null;
            }
            var role = (await _userManager.GetRolesAsync(user)).FirstOrDefault();
            if (role == null)
            {
                role = Role.Standard.ToString("f");
                await _userManager.AddToRoleAsync(user, role);
            }
            return new UserDto
            {
                Email = user.Email,
                Token = _tokenService.CreateToken(user),
                DisplayName = user.DisplayName,
                Role = role,
            };
        }

ApplicationRole:

public class ApplicationRole : IdentityRole<int>
    {
        public ApplicationRole() : base()
        {
        }
        public ApplicationRole(string name) : base(name)
        {
            Name = name;
        }
        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
        public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }
    }

in frontend:

  loadCurrentUser(token: string | null): Observable<void> {
    if (token === null) {
      this.currentUserSource.next(null);
      return of();
    }
    let headers = new HttpHeaders();
    headers = headers.set('Authorization', `Bearer ${token}`);
    return this.http.get(this.baseUrl   'account', { headers }).pipe(
      map((user: any) => {
        console.log("user = ", user);
        if (user) {
          localStorage.setItem('token', user.token);
          this.currentUserSource.next(user);
        }
      })
    );
  }

error

CodePudding user response:

I added builder.AddRoles<ApplicationRole>() and removed builder.Services.AddIdentity.

public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
{
    var builder = services.AddIdentityCore<ApplicationUser>();

    builder = new IdentityBuilder(builder.UserType, builder.Services);
            builder.AddSignInManager<SignInManager<ApplicationUser>>();
            builder.AddRoles<ApplicationRole>();
            builder.AddEntityFrameworkStores<ShopDbContext>();
            
            
   // rest of the code ...
}
  • Related