Beispiel #1
0
        public async Task <IActionResult> GetToken([FromBody] AuthBindingModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, model.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var roles = await UserMgr.GetRolesAsync(user);

                        return(generateToken(user, roles.FirstOrDefault()));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check Email & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user for given Email!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Beispiel #2
0
        public async Task <IActionResult> getinfoAsync()
        {
            string email = User.Claims.First(c => c.Type == "Email").Value;
            User   user  = await UserMgr.FindByEmailAsync(email);

            var roles = await UserMgr.GetRolesAsync(user);

            var returnuser = new UserResourceModel
            {
                UserName    = user.UserName,
                DisplayName = user.DisplayName,
                Role        = roles.FirstOrDefault().ToString(),
                Email       = user.Email,
            };

            return(Ok(returnuser));
        }
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await SignInMgr.PasswordSignInAsync(model.Email, model.Password, false, false);

                var user = await UserMgr.FindByEmailAsync(model.Email);

                var roles = await UserMgr.GetRolesAsync(user);

                if (result.Succeeded)
                {
                    if (roles.Contains("Admin"))
                    {
                        return(RedirectToAction("Index", "Admin"));
                    }

                    if (roles.Contains("Institution admin"))
                    {
                        return(RedirectToAction("Index", "Moderator"));
                    }

                    if (roles.Contains("Employee"))
                    {
                        return(RedirectToAction("Index", "Employee"));
                    }

                    if (roles.Contains("Donor"))
                    {
                        return(RedirectToAction("Index", "Donor"));
                    }
                }

                ModelState.AddModelError(string.Empty, "Slaptažodis arba prisijungimo vardas netinkamas");
            }

            return(View("IncorectLoging"));
        }