public Task AddLoginAsync(User user, UserLoginInfo login) { if (user == null) { throw new ArgumentNullException("user"); } if (login == null) { throw new ArgumentNullException("login"); } externalLoginRepository.Add(new ExternalLogin { UserId = user.Id, LoginProvider = login.LoginProvider, ProviderKey = login.ProviderKey }); return(Task.FromResult <object>(null)); }
public IHttpActionResult PostExternalLogin(ExternalLoginModel externalLogin) { // Validate request if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check that the corresponding user exists if (!_userRepository.Any(u => u.Id == externalLogin.UserID)) { throw new Exception("Unable to add the external login to the database, as it does not correspond to a user"); } //Set up new ExternalLogin object, populated from input externalLogin ExternalLogin dbExternalLogin = new ExternalLogin(); dbExternalLogin.Update(externalLogin); // Add the new ExternalLogin object to the DB _externalLoginRepository.Add(dbExternalLogin); // Save the changes in the database try { _unitOfWork.Commit(); } catch (Exception e) { throw new Exception("Unable to add the externalLogin to the database", e); } // Set externalLogin ID in ExternalLoginModel object with the ID // that was set in the DB externalLogin after db.SaveChanges externalLogin.ExternalLoginID = dbExternalLogin.ExternalLoginID; return(CreatedAtRoute("DefaultApi", new { id = externalLogin.ExternalLoginID }, externalLogin)); }
public ActionResult Facebook([FromBody] FacebookRequest request) { var response = new BaseResponse <SignInResponse>(); response.Data = new SignInResponse(); FacebookClient client = new FacebookClient(request.FacebookToken, "1112107155635637"); var FBresponse = client.Get("/me?fields=id,first_name,last_name,email", request.FacebookToken); string id = (string)FBresponse.SelectToken("id"); string firstName = (string)FBresponse.SelectToken("first_name"); string lastName = (string)FBresponse.SelectToken("last_name"); string email = (string)FBresponse.SelectToken("email"); if (string.IsNullOrWhiteSpace(email)) { response.Message = "Facebookta kayıtlı herhangi bir e-posta adresiniz olmadığı için oturum açamıyoruz, lütfen kayıt ol seçeneğini kullanın."; return(Ok(response)); } User user = null; var control = externalLoginRepo.FirstOrDefaultBy(x => x.ProviderKey == id && x.ProviderName == Provider.Facebook); if (control != null) { user = userRepo.GetById(control.UserID); } else { user = userRepo.FirstOrDefaultBy(x => x.Email == email); if (user == null) { //Yeni kullanıcı oluşturmamız lazım. var Username = email.Split("@")[0].ToString().ToLower(); while (userRepo.Any(x => x.Username == Username)) { Username += new Random().Next(1, 9).ToString(); } var Password = new Cryptography().EncryptString(Cryptography.GenerateKey(6)); List <ExternalLogin> externalLogins = new List <ExternalLogin>(); externalLogins.Add(new ExternalLogin { ProviderKey = id, ProviderName = Provider.Facebook }); var temp = new Entity.SystemUser.User { FirstName = firstName, LastName = lastName, Email = email, Role = Role.User, Username = Username, Password = Password, ExternalLogins = externalLogins }; userRepo.Add(temp); user = temp; } else { externalLoginRepo.Add(new ExternalLogin { UserID = user.Id, ProviderKey = id, ProviderName = Provider.Facebook }); } } var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes("this is my custom Secret key for authnetication"); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Username), new Claim("userID", user.Id.ToString()), new Claim(ClaimTypes.Role, user.Role.ToString()), new Claim("firstName", user.FirstName), new Claim("lastName", user.LastName) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); response.Data.Token = tokenHandler.WriteToken(token); return(Ok(response)); }