Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active August 29, 2015 14:06
Show Gist options
  • Save wadewegner/65941c6c302a4829d486 to your computer and use it in GitHub Desktop.
Save wadewegner/65941c6c302a4829d486 to your computer and use it in GitHub Desktop.
Trying to get salesforce auth with OWIN working
//
// POST: /Account/ExternalLoginConfirmation
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Manage");
}
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
await StoreAuthTokenClaims(user);
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
[Authorize]
public ActionResult Index()
{
var claimsIdentity = HttpContext.User.Identity as ClaimsIdentity;
if (claimsIdentity != null)
{
var claims = claimsIdentity.Claims;
}
return View();
}
app.UseSalesforceAuthentication(
clientId: "3MVG9xOCXq4ID1uECprHw9yA2eqjHLXvdWPz6d.ssZZsq7fwKp2QcqPGQTwnqaRsEZWtfbv3WsKQBMnY94xaQ",
clientSecret: "1716505757715014836");
private async Task StoreAuthTokenClaims(ApplicationUser user)
{
// Get the claims identity
ClaimsIdentity claimsIdentity =
await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (claimsIdentity != null)
{
// Retrieve the existing claims
var currentClaims = await UserManager.GetClaimsAsync(user.Id);
// Get the list of access token related claims from the identity
var tokenClaims = claimsIdentity.Claims
.Where(c => c.Type.StartsWith("urn:tokens:"));
// Save the access token related claims
foreach (var tokenClaim in tokenClaims)
{
if (!currentClaims.Contains(tokenClaim))
{
await UserManager.AddClaimAsync(user.Id, tokenClaim);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment