예제 #1
0
        public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var info = await Authentication.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(InternalServerError());
            }

            var user = new Core.Entities.ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
예제 #2
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new Core.Entities.ApplicationUser()
            {
                FirstName = model.FirstName,
                LastName  = model.LastName,
                UserName  = model.Email,
                Email     = model.Email,
                Activated = true
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                _accountService.UpdateUserRole(user.Id, "3");
                // _accountService.UpdateProjectsDeveloper(user.Id, "9");

                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.";
            }
            else
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }
예제 #3
0
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(new ChallengeResult(provider, this));
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return(InternalServerError());
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return(new ChallengeResult(provider, this));
            }

            Core.Entities.ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
                                                                                               externalLogin.ProviderKey));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                    OAuthDefaults.AuthenticationType);

                ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                     CookieAuthenticationDefaults.AuthenticationType);

                //testing role

                List <Claim>             roles      = oAuthIdentity.Claims.Where(c => c.Type == ClaimTypes.Role).ToList();
                AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName, Newtonsoft.Json.JsonConvert.SerializeObject(roles.Select(x => x.Value)));
                //end of testing

                //  AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName,);
                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable <Claim> claims   = externalLogin.GetClaims();
                ClaimsIdentity      identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

            return(Ok());
        }
예제 #4
0
파일: UserService.cs 프로젝트: userkimcs/ef
 public ServiceResult Update(Core.Entities.ApplicationUser user)
 {
     try
     {
         m_userRepository.Update(user);
         m_userRepository.SaveChanges();
         return(ServiceResult.Success);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Thong bao loi: " + ex.Data);
         return(ServiceResult.AddError("Lỗi"));
     }
 }
예제 #5
0
        public async Task <IHttpActionResult> UpdateProfile(RegisterBindingModel model)
        {
            // Encryptor encryp = new Encryptor();
            Core.Entities.ApplicationUser user = await UserManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));

            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;

            if (model.Password != null)
            {
                AspNetMvcSamplePasswordHasher ph = new AspNetMvcSamplePasswordHasher();
                user.PasswordHash = ph.HashPassword(model.Password);
            }
            IdentityResult result = await UserManager.UpdateAsync(user);

            return(Ok());
        }
예제 #6
0
        public async Task <IHttpActionResult> GetProfile()
        {
            Core.Entities.ApplicationUser user = await UserManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));

            if (user == null)
            {
                return(NotFound());
            }
            var registerModel = new RegisterBindingModel()
            {
                FirstName = user.FirstName,
                LastName  = user.LastName,
                Email     = user.Email
            };

            return(Ok(registerModel));
        }
예제 #7
0
        public async Task <IHttpActionResult> Create(UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new Core.Entities.ApplicationUser()
            {
                FirstName      = userDto.FirstName,
                LastName       = userDto.LastName,
                DateCreated    = DateTime.Now.Date,
                LastLoginDate  = DateTime.Now.Date,
                Email          = userDto.Email,
                Activated      = true,
                EmailConfirmed = true,
                PhoneNumber    = userDto.PhoneNumber,
                UserName       = userDto.UserName
            };

            IdentityResult result = await UserManager.CreateAsync(user, userDto.Password);

            if (result.Succeeded)
            {
                if (userDto.ProjectId == "" || userDto.RoleId == "")
                {
                    //   _accountService.UpdateProjectsDeveloper(user.Id, "9");
                    _accountService.UpdateUserRole(user.Id, "3");
                }
                else
                {
                    //  _accountService.UpdateProjectsDeveloper(user.Id, userDto.ProjectId);
                    _accountService.UpdateUserRole(user.Id, userDto.RoleId);
                }
            }
            else
            {
                return(Ok(new { errorMessage = result.Errors.First().ToString() }));
            }


            return(Ok());
        }
예제 #8
0
        public async Task <ManageInfoViewModel> GetManageInfo(string returnUrl, bool generateState = false)
        {
            Core.Entities.ApplicationUser user = await UserManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));

            if (user == null)
            {
                return(null);
            }

            List <UserLoginInfoViewModel> logins = new List <UserLoginInfoViewModel>();

            foreach (var linkedAccount in user.Logins)
            {
                logins.Add(new UserLoginInfoViewModel
                {
                    LoginProvider = linkedAccount.LoginProvider,
                    ProviderKey   = linkedAccount.ProviderKey
                });
            }

            if (user.PasswordHash != null)
            {
                logins.Add(new UserLoginInfoViewModel
                {
                    LoginProvider = LocalLoginProvider,
                    ProviderKey   = user.UserName,
                });
            }

            return(new ManageInfoViewModel
            {
                LocalLoginProvider = LocalLoginProvider,
                Email = user.UserName,
                Logins = logins,
                ExternalLoginProviders = GetExternalLogins(returnUrl, generateState)
            });
        }
예제 #9
0
파일: UserService.cs 프로젝트: userkimcs/ef
 public async Task <ServiceResult> UpdateAsync(Core.Entities.ApplicationUser user)
 {
     return(await Task.Run(() => Update(user)));
 }