I'm trying to create the following structure:
- WEB API which contains logic (different domain)
- IdentityServer4 API that provides auth (different domain)
- Angular 2 Client (different domain)
- Mobile APP.
Angular 2 client should have Twitter-Sign in button. Below is configuration for 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();
}
This configuration saves my access token and secret provided by twitter in my db.
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));
What I'm trying to find out is how should I implement my ExternalLoginCallback method to return tokens to Client (Angular or any other) that later will be used to authenticate in WEB API (or multiple APIs). For now I can see Auth cookie in reponse.