public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "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.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Obtenez des informations sur l’utilisateur auprès du fournisseur de connexions externe var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new User { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login")); } try { // Sign in the user with this external login provider if the user already has a login var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false); switch (result) { case SignInStatus.Success: return(RedirectToLocal(returnUrl)); case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false })); case SignInStatus.Failure: default: // If the user does not have an account, then prompt the user to create an account try { var user = new ApplicationUser { UserName = loginInfo.ExternalIdentity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value + " " + loginInfo.ExternalIdentity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value, Email = loginInfo.Email, FirstName = loginInfo.ExternalIdentity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value, Surname = loginInfo.ExternalIdentity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value }; var newUserResult = await UserManager.CreateAsync(user); if (newUserResult.Succeeded) { newUserResult = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (newUserResult.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal("/")); } } AddErrors(newUserResult); } catch { } return(View("ExternalLoginFailure")); } } catch { return(null); } }
public Task <IdentityResult> AddLoginAsync(string userId, UserLoginInfo loginInfo) { return(UserManager.AddLoginAsync(userId, loginInfo)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExtendedExternalLoginConfirmation model, string returnUrl) { using (var context = new ApplicationDbContext()) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "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")); } if (model.UserProfilePicture != null) { if (model.UserProfilePicture.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if (!(model.UserProfilePicture.ContentType == "image/jpeg" || model.UserProfilePicture.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } } byte[] data = new byte[model.UserProfilePicture.ContentLength]; model.UserProfilePicture.InputStream.Read(data, 0, model.UserProfilePicture.ContentLength); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, BirthDate = model.BirthDate, Firstname = model.Firstname, Surname = model.Surname, Photo = data }; var result = await UserManager.CreateAsync(user); var roleStore = new RoleStore <IdentityRole>(context); var roleManager = new RoleManager <IdentityRole>(roleStore); var userStore = new UserStore <ApplicationUser>(context); var userManager = new UserManager <ApplicationUser>(userStore); userManager.AddToRole(user.Id, "Employee"); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // Send an email with this link //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account"); ViewBag.Message = "Check your email and confirm your account, you must be confirmed " + "before you can log in."; return(View("Info")); //return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); } }
public async Task <IdentityResult> AddLoginAsync(string userId, UserLoginInfo login) { var result = await _AppUserManager.AddLoginAsync(userId, login); return(result); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login", new { returnUrl = returnUrl })); } // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(loginInfo.Login); if (user != null) { user.Claims = loginInfo.ExternalIdentity.Claims.ToList(); await SignInAsync(user, isPersistent : false); } else { // If the user does not have an account, then create an account user = new AzureIdentityUser() { UserName = loginInfo.Email, Email = loginInfo.Email, Claims = loginInfo.ExternalIdentity.Claims.ToList() }; IdentityResult result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); } } } Uri uri = null; if (string.IsNullOrEmpty(returnUrl) == false) { uri = new Uri(returnUrl); } JwtTokenHelper jwtTokenHelper = new JwtTokenHelper(); List <Claim> claims = new List <Claim>(); foreach (Claim claim in loginInfo.ExternalIdentity.Claims) { switch (claim.Type) { case ClaimTypes.Email: claims.Add(claim); break; case ClaimTypes.GivenName: claims.Add(claim); break; case ClaimTypes.Surname: claims.Add(claim); break; case ClaimTypes.Expiration: claims.Add(claim); break; case "urn:linkedin:accesstoken": claims.Add(claim); break; default: break; } } Claim expClaim = claims.Where(c => c.Type == ClaimTypes.Expiration).FirstOrDefault(); DateTime dExpires = DateTime.Now.AddHours(12); TimeSpan?expires = null; if (expClaim != null) { expires = TimeSpan.Parse(expClaim.Value); if (expires.HasValue) { dExpires = DateTime.Now.Add(expires.Value); } } string jwtToken = jwtTokenHelper.GenerateJwtToken(loginInfo.Login.LoginProvider, uri?.Host, claims, null, dExpires); UriBuilder uriBuilder = new UriBuilder(returnUrl); NameValueCollection queryCollection = HttpUtility.ParseQueryString(uriBuilder.Query); queryCollection["access_token"] = jwtToken; uriBuilder.Query = queryCollection.ToString(); return(RedirectToAction("OAuth2Confirmation", new { returnUrl = uriBuilder.ToString() })); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "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.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { //Assign Role to user Here await this.UserManager.AddToRoleAsync(user.Id, model.UserRole); //Ends Here // // Add user to user table // User newUser = new User(); newUser.Active = 1; newUser.EmailAddress = model.Email; newUser.Id = user.Id; newUser.Role = model.UserRole; UserBusinessLayer ubl = new UserBusinessLayer(); ubl.Add(newUser); switch (model.UserRole) { case "Amateur Artist": Artist newArtist = new Artist(); newArtist.UserID = newUser.UserID; ArtistBusinessLayer abl = new ArtistBusinessLayer(); abl.Add(newArtist); break; case "Business Owner": Business newBusiness = new Business(); newBusiness.UserID = newUser.UserID; BusinessBusinessLayer bbl = new BusinessBusinessLayer(); bbl.Add(newBusiness); break; default: // Admin Role?? break; } await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login")); } if (loginInfo.Login.LoginProvider == "Facebook") { var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie); var access_token = identity.FindFirstValue("FacebookAccessToken"); var fb = new FacebookClient(access_token); dynamic myInfo = fb.Get("/me?fields=email"); loginInfo.Email = myInfo.email; } var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false); switch (result) { case SignInStatus.Success: return(RedirectToLocal(returnUrl)); case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false })); case SignInStatus.Failure: default: var user = new ApplicationUser { UserName = loginInfo.DefaultUserName, Email = loginInfo.Email }; if (UserManager.FindByEmail(user.Email) == null) { var resultt = await UserManager.CreateAsync(user); if (resultt.Succeeded) { resultt = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (resultt.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } } else { var resultt = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (resultt.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } return(RedirectToLocal(returnUrl)); } //else //{ // ViewBag.ReturnUrl = returnUrl; // ViewBag.LoginProvider = loginInfo.Login.LoginProvider; // var newUser = new ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email }; // var newResult = await UserManager.CreateAsync(newUser, "Nhy6&*()"); // await SignInManager.PasswordSignInAsync(loginInfo.Email, "Nhy6&*()", // false, shouldLockout: false); // return RedirectToAction("List", "Event"); //} //switch (result) //{ // case SignInStatus.Success: // return RedirectToLocal(returnUrl); // case SignInStatus.LockedOut: // return View("Lockout"); // case SignInStatus.RequiresVerification: // return RedirectToAction("SendCode", new {ReturnUrl = returnUrl, RememberMe = false}); // case SignInStatus.Failure: // default: // // If the user does not have an account, then prompt the user to create an account // ViewBag.ReturnUrl = returnUrl; // ViewBag.LoginProvider = loginInfo.Login.LoginProvider; // var newUser = new ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email }; // var newResult = await UserManager.CreateAsync(newUser, "Nhy6&*()"); // await SignInManager.PasswordSignInAsync(loginInfo.Email, "Nhy6&*()", // false, shouldLockout: false); // return RedirectToLocal(returnUrl); // //return View("ExternalLoginConfirmation", // // new ExternalLoginConfirmationViewModel { UserName = loginInfo.ExternalIdentity.Name ,Email = loginInfo.Email }); //} }