Esempio n. 1
0
    //Method to authenticate a user
    protected void SignIn(object sender, EventArgs e)
    {
        //Instantiate a new UserManager object from the IdentityEF class that we imported.
        //This object is responsible for reading/writing data related to users of the application.
        var userManager = new IdentityEF.UserManager();


        //Call the Find method of the UserManager to attempt to locate the user credentials in the database
        //If the credentials are not found, the user variable will be null
        var user = userManager.Find(UserName.Text, Password.Text);

        //Create a boolean variable that denotes whether the user authentication should persist (the cookie does not expire)
        bool rememberme = RememberMe.Checked;

        //If the user variable is not null (meaning credentials are valid), sign the user in.
        if (user != null)
        {
            //Get a reference to the OWIN authentication middleware that will handle user authentication
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            //Create a new ClaimsIdentity for the user
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            //Use the authentication mamanger to sign in the user.
            //Pass in a new AuthenticationProperties object (allows for setting various properties of authentication.
            //Pass in the ClaimsIdentity object created above.
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = rememberme
            }, userIdentity);


            //Redirect the user to the Profile page where they can add/modify additional profile variables.
            Response.Redirect("~/Account/Profile.aspx");
        }
        else
        {
            StatusText.Text   = "Invalid username or password.";
            StatusBox.Visible = true;
        }
    }
    //Method to authenticate a user
    protected void SignIn(object sender, EventArgs e)
    {
        //Instantiate a new UserManager object from the IdentityEF class that we imported.
        //This object is responsible for reading/writing data related to users of the application.
        var userManager = new IdentityEF.UserManager();


        //Call the Find method of the UserManager to attempt to locate the user credentials in the database
        //If the credentials are not found, the user variable will be null
        var user = userManager.Find(txtLoginEmail.Value, txtLoginPassword.Value);

        //Create a boolean variable that denotes whether the user authentication should persist (the cookie does not expire)
        bool rememberme = chkRememberMe.Checked;

        //If the user variable is not null (meaning credentials are valid), sign the user in.
        if (user != null)
        {
            //Get a reference to the OWIN authentication middleware that will handle user authentication
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            //Create a new ClaimsIdentity for the user
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            //Use the authentication mamanger to sign in the user.
            //Pass in a new AuthenticationProperties object (allows for setting various properties of authentication.
            //Pass in the ClaimsIdentity object created above.
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = rememberme
            }, userIdentity);


            string        constring     = WebConfigurationManager.ConnectionStrings["5050_Viavago"].ConnectionString;
            SqlConnection con           = new SqlConnection(constring);
            string        selectCommand = "SELECT UserID FROM Users WHERE UserName = @UserName;";
            SqlCommand    cmdSelect     = new SqlCommand(selectCommand, con);
            cmdSelect.Parameters.AddWithValue("@UserName", txtLoginEmail.Value);
            DataTable table = new DataTable();
            try
            {
                con.Open();
                SqlDataReader reader = cmdSelect.ExecuteReader();
                table.Load(reader);
            }
            catch (Exception err)
            {
                lblStatus.Text = err.Message;
            }
            finally
            {
                con.Close();
            }

            var UserId = (Int32)table.Rows[0]["UserID"];
            Session["UserId"] = UserId;
            //Redirect the user to the Profile page where they can add/modify additional profile variables.
            Response.Redirect("~/EditProfile.aspx");
        }
        else
        {
            lblStatus.Text = "Invalid username or password.";
        }
    }