Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
string redirectUri = string.Empty;
OAuthClient oAuthClient = null;
if (error != null)
return BadRequest(Uri.EscapeDataString(error));
if (!User.Identity.IsAuthenticated)
return new ChallengeResult(provider, this);
var redirectUriValidationResult = ValidateClientAndRedirectUri(this.Request, ref redirectUri, ref oAuthClient);
if (!string.IsNullOrWhiteSpace(redirectUriValidationResult))
return BadRequest(redirectUriValidationResult);
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
if (externalLogin == null)
return InternalServerError();
if (externalLogin.LoginProvider != provider)
return new ChallengeResult(provider, this);
ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey));
bool hasRegistered = user != null;
if (!hasRegistered)
IdentityResult identityResult = RegisterExternalUser(externalLogin, ref user);
if (!identityResult.Succeeded)
return GetErrorResult(identityResult);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, "JWT");
//generate access token response
var accessTokenResponse = GenerateLocalAccessTokenResponse(oAuthIdentity, oAuthClient.Id, oAuthClient.AudienceId);
redirectUri = string.Format("{0}#access_token={1}&token_type={2}&expires_in={3}",
return Redirect(redirectUri);

This comment has been minimized.

Copy link

@telatella telatella commented Feb 19, 2016

Hi, thanks for sharing this. I've just been checking the comments from the original post and then got referred to this. Can you please provide the implementation for the helper methods : ValidateClientAndRedirectUri(this.Request, ref redirectUri, ref oAuthClient) and RegisterExternalUser


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.