コード例 #1
0
        public IActionResult Login(string username, string password)
        {
            try
            {
                LdapService _ldapService = new LdapService(_ldapconfig);
                var         appUser      = _ldapService.Login(username, password);

                if (appUser != null && appUser.IsAuthenticated)
                {
                    // insert user
                    if (!_eventsRepo.UserExists(username))
                    {
                        _eventsRepo.SaveUser(username, "");
                    }

                    Response.StatusCode = 200;
                    return(Ok(Response.StatusCode));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
コード例 #2
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (ldapService.Login(model.Username, model.Password))
                    {
                        var userClaims = new List <Claim>
                        {
                            new Claim("username", model.Username),
                            new Claim(ClaimsIdentity.DefaultNameClaimType, model.Username)
                        };

                        var principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims, ldapService.GetType().Name, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType));
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                        return(Redirect(returnUrl ?? Request.PathBase + "/"));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return(View(model));
        }