Exemple #1
0
        public async Task <IActionResult> Login(string u, string p)
        {
            if (LDAPUtil.Validate(u, p))
            {
                var identity  = new ClaimsIdentity(new MyIdentity(u));
                var principal = new ClaimsPrincipal(identity);

                await HttpContext.Authentication.SignInAsync(LDAPUtil.CookieName, principal);

                return(RedirectToAction("Index", "Home"));
            }
            return(View());
        }
Exemple #2
0
        public static void Login()
        {
            string username = "******";
            string password = "******";

            var loginFlag = LDAPUtil.Validate(username, password);

            if (loginFlag)
            {
                System.Console.WriteLine("User validate successfully!");
            }
            else
            {
                System.Console.WriteLine("User validate unsuccessfully!");
            }

            System.Console.ReadLine();
        }
Exemple #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;

            await Task.Delay(200);

            if (ModelState.IsValid)
            {
                //var user = AuthenticateUser(Input.ID, Input.Password);

                if (!LDAPUtil.Validate(Input.ID, Input.Password)) //驗證失敗
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }

                // conn 取的登入者資料
                var conn = new DapperConnection.ConnectionOptions();
                Configuration.GetSection(DapperConnection.ConnectionOptions.Position).Bind(conn);

                string sqlStr = string.Format(@" SELECT TOP 1 
                                        '{0}' as ID,
                                        FST_Name + ' ' + LST_Name as FullName ,
                                        case ISNULL(FST_Name, '')
	                                    when '' then ISNULL(LST_Name, '')
	                                    else FST_Name end FST_Name
                                        from EMP_Profile where EMP_Account like '%{0}%'", Input.ID);

                using (var con = new Microsoft.Data.SqlClient.SqlConnection(conn.EmpServerContext))
                {
                    Input_conn = con.Query <InputModel>(sqlStr).ToList();
                    //List_EmpProfile = con.Query<EmpProfile>(sqlStr, new { OrderDetailID = 1 }).ToList();
                }


                // 之後建立員工資料表 再加上 EMP info
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, Input.ID),
                    new Claim(ClaimTypes.Role, "Administrator"),
                    new Claim("FstName", Input_conn[0].FST_Name),
                    new Claim(ClaimTypes.Surname, Input_conn[0].FullName),
                };

                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    authProperties);

                _logger.LogInformation("User {Email} logged in at {Time}.",
                                       Input.ID, DateTime.UtcNow);

                return(LocalRedirect(Url.GetLocalUrl(returnUrl)));
            }
            // Something failed. Redisplay the form.
            return(Page());
        }