Beispiel #1
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Invalid username/password");
                return(View());
            }
            var user = new User("", "", model.Username, model.Password);

            using (var context = new BPContext())
            {
                if (UserAuthentication.IsValidUser(user, context))
                {
                    var temp   = UserAuthentication.GetUser(user.Username, user.Password, context);
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, temp.FirstName),
                        new Claim("FullName", temp.ToString()),
                        new Claim(ClaimTypes.Role, temp.Access.ToString()),
                    };

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

                    var authProperties = new AuthenticationProperties
                    {
                        //AllowRefresh = <bool>,
                        // Refreshing the authentication session should be allowed.

                        //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                        // The time at which the authentication ticket expires. A
                        // value set here overrides the ExpireTimeSpan option of
                        // CookieAuthenticationOptions set with AddCookie.

                        //IsPersistent = true,
                        // Whether the authentication session is persisted across
                        // multiple requests. Required when setting the
                        // ExpireTimeSpan option of CookieAuthenticationOptions
                        // set with AddCookie. Also required when setting
                        // ExpiresUtc.

                        //IssuedUtc = <DateTimeOffset>,
                        // The time at which the authentication ticket was issued.

                        //RedirectUri = <string>
                        // The full path or absolute URI to be used as an http
                        // redirect response value.
                    };

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

                    if (model.ReturnURL != null && Url.IsLocalUrl(model.ReturnURL))
                    {
                        return(Redirect(model.ReturnURL));
                    }
                    return(RedirectToAction("GetPlayerRankings", "Performances"));
                }
                else
                {
                    return(View("Login"));
                }
            }
        }
Beispiel #2
0
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = textBoxUsername.Text.Trim();
                string password = textBoxPassword.Text.Trim();
                errorProvider1.Clear();
                labelMessage.Text = string.Empty;

                if (userName == string.Empty && password == string.Empty)
                {
                    labelMessage.Text = MessageManager.GetMessage("1", false);
                    errorProvider1.SetError(textBoxUsername, MessageManager.GetMessage("1", false));
                    return;
                }

                if (userName == string.Empty)
                {
                    labelMessage.Text = MessageManager.GetMessage("1", false);
                    errorProvider1.SetError(textBoxUsername, MessageManager.GetMessage("1", false));

                    return;
                }

                if (password == string.Empty)
                {
                    labelMessage.Text = MessageManager.GetMessage("1", false);
                    errorProvider1.SetError(textBoxPassword, MessageManager.GetMessage("1", false));

                    return;
                }

                int             userId = 0;
                Common.UserRole role   = new Common.UserRole();

                bool validUser = userAuthentication.IsValidUser(userName, password, out userId, out role);
                Logger.WriteTrace("Login", "Username : "******"Success : " + validUser.ToString());

                if (!validUser)
                {
                    MessageManager.DisplayCustomMessage("Invalid user Id or password.");
                }
                else
                {
                    if (checkBoxRemember.Checked)
                    {
                        SavePreference(true);
                    }
                    else
                    {
                        SavePreference(false);
                    }

                    SessionParameters.UserID   = userId;
                    SessionParameters.UserName = userName;
                    SessionParameters.UserRole = role;
                    (new Users()).UpdateLastLoginDate(SessionParameters.UserID);

                    panelLogin.Hide();
                    panelOpen.Show();

                    InitScreenData();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }