Пример #1
0
        public async Task <ActionResult> Callback(string ticket)
        {
            try
            {
                var response = await HttpService.UserCampusNetAuthHttpRequestAsync(ticket);

                var valdation = response.Split("\n");
                if (valdation[0].Equals("no"))
                {
                    return(Unauthorized("Login failed, reply was:" + valdation[0]));
                }

                var username = valdation[1];
                var email    = $"{username}@student.dtu.dk";
                var appUser  = _userManager.Users.FirstOrDefault(r => r.Email == email);

                if (appUser != null)
                {
                    await _signInManager.SignInAsync(appUser, false);

                    var token = JwtHelperService.GenerateJwtToken(username, appUser, _configuration);
                    return(Redirect($"https://freelance-portal.herokuapp.com/?token={token}"));
                }

                {
                    var user = new StudentModel
                    {
                        UserName      = username,
                        Firstname     = username,
                        Role          = Role.Student,
                        Lastname      = "",
                        Email         = email,
                        LocationModel = new LocationModel {
                            Street = "", City = "", Number = "", Zip = ""
                        },
                        CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now
                    };
                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        return(BadRequest(string.Join(",",
                                                      result.Errors?.Select(error => error.Description) ??
                                                      throw new InvalidOperationException())));
                    }

                    await _signInManager.SignInAsync(user, false);

                    var token = JwtHelperService.GenerateJwtToken(username, user, _configuration);
                    return(Redirect($"https://freelance-portal.herokuapp.com/?token={token}"));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(500));
            }
        }
Пример #2
0
        public async Task <IActionResult> GoogleAuth([FromBody] TokenModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Unauthorized("Google token is invalid"));
            }
            var validPayload = await GoogleJsonWebSignature.ValidateAsync(model.access_token);

            if (validPayload == null)
            {
                return(Unauthorized("Google token is invalid"));
            }
            var appUser = _userManager.Users.FirstOrDefault(r => r.Email == validPayload.Email);

            if (appUser != null)
            {
                await _signInManager.SignInAsync(appUser, false);

                var token    = JwtHelperService.GenerateJwtToken(validPayload.Name, appUser, _configuration);
                var rootData = new LoginResponseModel(token);
                return(Ok(rootData));
            }

            {
                var user = new StudentModel
                {
                    Firstname     = validPayload.GivenName, Lastname = validPayload.FamilyName, Role = Role.Student,
                    UserName      = validPayload.GivenName,
                    LocationModel = new LocationModel {
                        Street = "", City = "", Number = "", Zip = ""
                    },
                    Email     = validPayload.Email, Logo = validPayload.Picture,
                    CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now
                };
                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    return(BadRequest(string.Join(",",
                                                  result.Errors?.Select(error => error.Description) ?? throw new InvalidOperationException())));
                }

                await _signInManager.SignInAsync(user, false);

                var token    = JwtHelperService.GenerateJwtToken(validPayload.Name, user, _configuration);
                var rootData = new LoginResponseModel(token);
                return(Ok(rootData));
            }
        }
Пример #3
0
        public async Task <ActionResult> Login([FromBody] LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);

                if (!result.Succeeded)
                {
                    return(StatusCode((int)HttpStatusCode.Unauthorized, "Bad Credentials"));
                }
                var appUser  = _userManager.Users.FirstOrDefault(r => r.UserName == model.UserName);
                var token    = JwtHelperService.GenerateJwtToken(model.UserName, appUser, _configuration);
                var rootData = new LoginResponseModel(token);
                return(Ok(rootData));
            }

            var errorMessage =
                string.Join(", ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));

            return(BadRequest(errorMessage ?? "Bad Request"));
        }