public async Task <IActionResult> ExternalLoginCallback() { // read external identity from the temporary cookie var result = await HttpContext.AuthenticateAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme); if (result?.Succeeded != true) { throw new Exception("External authentication error"); } var externalUser = new ExternalUser(result.Properties.Items["scheme"], result.Principal); var provider = externalUser.Provider; var userId = externalUser.UniqueIdentifier(); var user = _userRepository.GetUser(userId, provider); if (user == null) { // this sample simply auto-provisions new external user // another common approach is to start a registrations workflow first var context = _interaction.GetAuthorizationContextAsync(result.Properties.Items["returnUrl"]); var tenant = _tenantRepository.GetTenantByName(context.Result.Tenant); user = externalUser.ProvisionUser(userId, tenant.Id.ToString()); _userRepository.AddUser(user); } // if the external provider issued an id_token, we'll keep it for signout AuthenticationProperties props = null; var id_token = AuthenticationTokenExtensions.GetTokenValue(result.Properties, "id_token"); if (id_token != null) { props = new AuthenticationProperties(); props.StoreTokens(new[] { new AuthenticationToken { Name = "id_token", Value = id_token } }); } // issue authentication cookie for user await _events.RaiseAsync(new UserLoginSuccessEvent(provider, userId, user.SubjectId, user.Username)); await HttpContext.SignInAsync(user.SubjectId, user.Username, provider, props, externalUser.SessionIdClaim(), user.Claims.First(x => x.Type == ClaimType.TenantId)); // delete temporary cookie used during external authentication await HttpContext.SignOutAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme); // validate return URL and redirect back to authorization endpoint or a local page var returnUrl = result.Properties.Items["returnUrl"]; if (_interaction.IsValidReturnUrl(returnUrl) || Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } return(Redirect("~/")); }
public ActionResult AddTenant(TblTenant objTenant, HttpPostedFileBase file) { try { if (ModelState.IsValid) { if (objTenant.TenantId == 0) { var result = tr.GetTenantByName(objTenant.TenantName); if (!result) { TempData["Message"] = "Tenant Already Exist"; return(View(objTenant)); } } if (file != null) { var logoURL = System.Configuration.ConfigurationManager.AppSettings["LogoURL"]; var logoPhysicalURL = System.Configuration.ConfigurationManager.AppSettings["logoPhysicalURL"]; string filePhysicalPath = System.IO.Path.Combine(logoPhysicalURL + "\\" + objTenant.TenantName + ".jpg"); string path = System.IO.Path.Combine(logoURL + "\\" + objTenant.TenantName + ".jpg"); file.SaveAs(filePhysicalPath); objTenant.Logo = path; } int rows = 0; if (objTenant.TenantId == 0) { rows = tr.AddTenant(objTenant); } else { rows = tr.EditTenants(objTenant); } if (rows != 0) { return(RedirectToAction("AddTenantUser", new { @id = objTenant.TenantId })); } else { return(View(objTenant)); } } return(View(objTenant)); } catch (Exception ex) { newException.AddException(ex); return(View()); } }