E
E
EvilAvenger2017-03-24 15:34:02
Angular
EvilAvenger, 2017-03-24 15:34:02

How to implement the following bundle - IdentityServer4 + WebApi + Angular2 + External Auth?

Hello, I'm trying to do the following:
WEB API separate domain
IdentityServer4 API separate domain
Angular 2 Client separate
Mobile APP domain.
Angular 2 client authorizes with an External service (Twitter)

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(LogLevel.Debug);

        app.UseCors("CorsPolicy");

        app.UseIdentity();
        app.UseIdentityServer();

        //after identity before mvc
        app.UseTwitterAuthentication(new TwitterOptions
        {
            AuthenticationScheme = "Twitter",
            DisplayName = "Twitter",
            SignInScheme = "Identity.External",
            ConsumerKey = "key",
            ConsumerSecret = "secret",
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            SaveTokens = true,
        });
        app.UseMvc();
    }

The code below saves my token from twitter to the database:
bool result = false;
        var info = await signInManager.GetExternalLoginInfoAsync();
        if (info != null)
        {
            var tempUser = info.Principal;
            var claims = tempUser.Claims.ToList();

            var userIdClaim = claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
            var email = claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email);

            if (userIdClaim != null)
            {
               var isRegistered = await IsUserRegistered(info.LoginProvider, info.ProviderKey);
                if (!isRegistered && email != null)
                {
                    var user = new ApplicationUser { UserName = userIdClaim.Value, Email = email.Value };
                    var userCreated = await userManager.CreateAsync(user);
                    isRegistered = userCreated.Succeeded;

                    if (isRegistered)
                    {
                        var addLoginresult = await userManager.AddLoginAsync(user, info);
                        isRegistered = addLoginresult.Succeeded;
                        if (isRegistered)
                        {
                            await signInManager.SignInAsync(user, isPersistent: false);
                        }
                    }
                }

                if (isRegistered)
                {
                    var succeded = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
                    if (succeded.Succeeded)
                    {
                        IdentityResult updateResult = await signInManager.UpdateExternalAuthenticationTokensAsync(info);
                        result = updateResult.Succeeded;
                    }
                }
            }
        }

        if (!result)
        {
            await signInManager.SignOutAsync();
        }

        return Redirect(System.Net.WebUtility.UrlDecode(returnUrl));

At the moment, it is not clear how I can get a valid token from this, which I could return to the client (at the moment the cookie is being returned), with the token later authorized in other apishkas or resources.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EvilAvenger, 2017-03-27
@EvilAvenger

For now, I haven't been able to figure it out.
But perhaps the problem is that I receive an external token from twitter,
and I need to somehow get the internal token issued by ID4 and already use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question