コード例 #1
0
        public async Task <AuthResult <int> > AttemptLoginWithFacebookAsync(FacebookLoginDto dto)
        {
            var user = await _userManager.FindByEmailAsync(dto.Email);

            if (user == null)
            {
                user = new ImageHubUser()
                {
                    UserName = dto.Email,
                    Email    = dto.Email
                };

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    return(new AuthResult <int>()
                    {
                        Successful = false,
                        Errors = result.Errors.Select(err => err.Description)
                    });
                }

                await _signInManager.SignInAsync(user, isPersistent : true);
            }

            // we add the id of the user role only so that no meaningful info is sent out to the client
            return(new AuthResult <int>()
            {
                Successful = true,
                UserId = user.Id
            });
        }
コード例 #2
0
        public async Task <AuthResult <int> > AttemptRegisterAsync(LoginDto register)
        {
            var user = await _userManager.FindByNameAsync(register.Username);

            if (user != null)
            {
                throw new ApplicationException("User alread exists!");
            }


            // a more approriate way would be to configure a mapping in Automapper
            user          = new ImageHubUser();
            user.UserName = register.Username;

            var result = await _userManager.CreateAsync(user, register.Password);

            if (!result.Succeeded)
            {
                return(new AuthResult <int>()
                {
                    Successful = false,
                    Errors = result.Errors.Select(err => err.Description)
                });
            }

            var signInResult = await _signInManager.PasswordSignInAsync(user, register.Password, false, false);

            // we add the id of the user role only so that no meaningful info is sent out to the client
            return(new AuthResult <int>()
            {
                Successful = true,
                UserId = user.Id
            });
        }