Open Closed

getting identity token after user sign-in #2912


User avatar
0
shobhit created
  • ABP Framework version: v4.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

i am creating new api endpoint which allowes google auth token to be passed as input paramenter. my api method do all business check as per below code to check the token and register the user. Now question is how i can send idenitty auth token back to user so that it can be used with other api calls.

public async Task<string> RegisterExternalUserAsync(string LoginProvider, string token)
{
    //ClaimsPrincipal claimsPrincipal = new();
    IRestResponse restResponse = new RestResponse();

    if (LoginProvider == "Google")
    {
        var client = new RestClient("https://www.googleapis.com/oauth2/v2/userinfo");


        client.Timeout = -1;

        var request = new RestRequest(Method.GET);
        request.AddHeader("Accept", "application/json");
        client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", token));
        restResponse = client.Execute(request);
    }

    if(restResponse == null)
    {
        return "Invalid Token";
    }

    CustomUserData data = JsonConvert.DeserializeObject<CustomUserData>(restResponse.Content);

    var result = await _signInManager.ExternalLoginSignInAsync(
        LoginProvider,
        data.id,
        isPersistent: true,
        bypassTwoFactor: true
    );

    if (!result.Succeeded)
    {
        var user = new Volo.Abp.Identity.IdentityUser(GuidGenerator.Create(), data.email, data.email, CurrentTenant.Id);

        await _userManager.CreateAsync(user);
        await _userManager.AddDefaultRolesAsync(user);

        if (!user.EmailConfirmed)
        {
            var clientUrl = _configuration["App:ClientUrl"];

            SendEmailConfirmationTokenDto emailConfirmationTokenDto = new()
            {
                AppName = "MVC",
                Email = data.email,
                ReturnUrl = clientUrl
            };

            await _accountAppService.SendEmailConfirmationTokenAsync(emailConfirmationTokenDto);
        }

        var userLoginAlreadyExists = user.Logins.Any(x =>
            x.TenantId == user.TenantId &&
            x.LoginProvider == LoginProvider &&
            x.ProviderKey == data.id);

        if (!userLoginAlreadyExists)
        {
            user.AddLogin(new Microsoft.AspNetCore.Identity.UserLoginInfo(
                    LoginProvider,
                    data.id,
                    LoginProvider
                )
            );

            await _userManager.UpdateAsync(user);
        }

        await _signInManager.SignInAsync(user,true,null);

        //TODO: return identity token back to user
    }
    else
    {   
        var identityUser = await _userManager.FindByEmailAsync(data.email);
        await _signInManager.SignInAsync(identityUser, true,null);

        //TODO: return identity token back to user
    }

    return string.Empty;
}


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    This is an MVC page, it cannot pass parameters to the Angular app. Angular can only call the API to get info.

  • User Avatar
    0
    shobhit created

    Hi @mliming, this is custom api implementation and this api used by our mobile app. Now challenge is, how to send back token to mobile app so that user can continue in the app.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can consider adding custom to the claims. but stores a value corresponding to it, since claims may be plaintext to the client.

    https://docs.abp.io/en/abp/latest/Authorization#claims-principal-factory

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38