Example #1
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //declare the collection of users
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            //declare the user manager
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            //try to find the user
            IdentityUser user = manager.Find(txtEmpNum.Text, txtPassword.Text);
            if (user == null)
                lblStatus.Text = "Username or Password is incorrect";
            else
            {
                if (txtEmpNum.Text == "Administrator")
                {
                    IdentityResult userResult = manager.AddToRole(user.Id, "Admin");
                }
                //add user to role
                //authenticate user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(userIdentity);
                Response.Redirect("~/MainPage.aspx");

            }
        }
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            // Default UserStore constructor uses the default connection string named: DefaultConnection
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);
            //IdentityResult IdUserResult;

            var user = new IdentityUser() { UserName = UserName.Text };
            IdentityResult result = manager.Create(user, Password.Text);

            if (result.Succeeded)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);

               // IdUserResult = manager.AddToRole(manager.FindByName(user.UserName).Id, "member");

                Session["uPass"] = Password.Text;
                Response.Redirect("~/WebForm2.aspx");
                // StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
            }
            else
            {
                StatusMessage.Text = result.Errors.FirstOrDefault();
            }
        }
Example #3
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // Default UserStore constructor uses the default connection string named: DefaultConnection
                var userStore = new UserStore<IdentityUser>();
                var manager = new UserManager<IdentityUser>(userStore);

                var user = new IdentityUser() { UserName = txtUsername.Text };
                IdentityResult result = manager.Create(user, txtPassword.Text);

                if (result.Succeeded)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                    Response.Redirect("/user/index.aspx");

                }
                else
                {
                    lblStatus.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (Exception d)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                // Default UserStore constructor uses the default connection string named: DefaultConnection
                var userStore = new UserStore<IdentityUser>();
                var manager = new UserManager<IdentityUser>(userStore);

                var user = new IdentityUser() { UserName = txtUsername.Text };

                IdentityResult result = manager.Create(user, txtPassword.Text);

                if (result.Succeeded)
                {
                    //lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
                    //lblStatus.CssClass = "label label-success";
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                    Response.Redirect("admin/main-menu.aspx");
                }
                else
                {
                    lblStatus.Text = result.Errors.FirstOrDefault();
                    lblStatus.CssClass = "label label-danger";
                }
            }
            catch (Exception q)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            // create new userStore and userManager objects
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);

            // search for and create a new user object
            var user = userManager.Find(UserNameTextBox.Text, PasswordTextBox.Text);

            // if a match is found for the user
            if(user != null)
            {
                // authenticate and login our new user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                // Sign the user
                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);

                // Redirect to Main Menu
                Response.Redirect("~/Contoso/MainMenu.aspx");
            }
            else
            {
                // throw an error to the AlertFlash div
                StatusLabel.Text = "Invalid Username or Password";
                AlertFlash.Visible = true;
            }
        }
Example #6
0
        protected void SignIn(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            IdentityUser user = userManager.Find(tbUsername.Text, tbPassword.Text);

            //if user info is found
            if (user != null)
            {
                //create cookie
                IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                ClaimsIdentity userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                //sign in
                authenticationManager.SignIn(new AuthenticationProperties {IsPersistent = false}, userIdentity);

                var returnUrl = Request.QueryString["returnUrl"];
                //if user came from different page, redirect to that one. Otherwise redirect to main page.
                Response.Redirect(returnUrl ?? "~/default.aspx");
            }
            //if not, show error message.
            else
            {
                lblConfirmationText.Text = "Invalid username or password.";
            }
        }
        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            //crete new userStore and userManager objects
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);

            var user = new IdentityUser()
            {
                UserName = UserNameTextBox.Text,
                PhoneNumber = PhoneNumberTextBox.Text,
                Email = EmailTextBox.Text
            };
            //create new user in the dbb and store the resukt
            IdentityResult result = userManager.Create(user, PasswordTextBox.Text);
            //check if succesfully registered
            if (result.Succeeded)
            {
                //authenticate and login new user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);//store info in session

                //sign in
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);

                //redirect to main menu
                Response.Redirect("~/Secured/TodoList.aspx");

            }
            else
            {
                //display error in the AlertFlash div
                StatusLabel.Text = result.Errors.FirstOrDefault();
                AlertFlash.Visible = true;
            }
        }
Example #8
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            // Default UserStore constructor uses the default connection string named: DefaultConnectionEF
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);

            var user = new IdentityUser() { UserName = txtUName.Text };
            user.Email = txtEmail.Text;
            user.PhoneNumber = txtPhone.Text;
            IdentityResult result = manager.Create(user, txtPass.Text);

            if (result.Succeeded)
            {
                lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                Response.Redirect("/admin/main.aspx");

            }
            else
            {
                lblStatus.Text = result.Errors.FirstOrDefault();
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                //store user information into variables
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = userManager.Find(txtUsername.Text, txtPassword.Text);

                //if there is a current user
                if (user != null)
                {
                    //if the user is authenticated, redirect to products page
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("/admin/products.aspx", false);
                }
                else //if any fields are blank
                {
                    //show a message to the user
                    lblStatusMessage.Text = "Invalid username or password.";
                    lblStatusMessage.Visible = true;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/Error.aspx");
            }
        }
Example #10
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            //database connection not-authicating
            //System.Data.Entity.Core.EntityCommandExecutionException
            //System.Data.SqlClient.SqlException
            try
            {
                var user = userManager.Find(txtUsername.Text, txtPassword.Text);

                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("/admin/main.aspx");
                }
                else
                {
                    lblStatus.Text = "Invalid username or password.";
                }
            }
            catch (System.Data.Entity.Core.EntityCommandExecutionException ECEE) {
                Server.Transfer("/ErrorPage.aspx", true);
            }
            catch (System.Data.SqlClient.SqlException SqlE) {
                Server.Transfer("/ErrorPage.aspx", true);
            }
        }
Example #11
0
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);

            IdentityUser user = manager.Find(LoginUser.UserName, LoginUser.Password);

            if (user != null)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);

                if (Request.QueryString["ReturnUrl"] == null)
                    Response.Redirect("../Default.aspx");
                else
                    Response.Redirect(Request.QueryString["ReturnUrl"]);

            }
            else
            {
                LoginUser.FailureText = "Invalid User Name or Password";

            }
        }
Example #12
0
        public ActionResult Register(User user)
        {
            string temp = user.Password;
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);

            var user2 = new IdentityUser() { UserName = user.Username };
            IdentityResult result = manager.Create(user2, user.Password);

            if (result.Succeeded)
            {
                TempData["message"] = "Identity user create worked";

                //var temp2 =  this.ControllerContext.HttpContext;
                var authenticationManager = HttpContext.GetOwinContext().Authentication;
                //var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user2, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                //Response.Redirect("~/Login.aspx");
                //return View("Login");

                if (User.Identity.IsAuthenticated)
                {
                    TempData["message"] += "/n   User.Identity.IsAuthenticate working";
                }

                return View("Index");
            }
            else
            {
                TempData["message"] = "Failed: " + result.Errors.FirstOrDefault();
            }
            return View("Index");
        }
Example #13
0
        /**
          Login - authenticate entered user credientials.
         **/
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = userManager.Find(txtUsername.Text, txtPassword.Text);

                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("admin/bibleMenu.aspx");
                }
                else
                {
                    lblStatus.Text = "Invalid username or password.";
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/errors.aspx");
            }
        }
Example #14
0
 public ClaimsIdentity GenerateUserIdentity(UserManager<User> manager)
 {
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
     var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
     // Add custom user claims here
     return userIdentity;
 }
Example #15
0
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            // create new userStore and userManager objects
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);

            // Find the user
            var user = userManager.Find(UserNameTextBox.Text, PasswordTextBox.Text);

            // check if username and password combo exists
            if (user != null)
            {
                // authenticate and login new user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);

                // redirect to the Main Menu page
                Response.Redirect("~/game.aspx");
            }
            else
            {
                StatusLabel.Text = "Invalid Username or Password";
                AlertFlash.Visible = true;
            }
        }
Example #16
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //try
            //{
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = userManager.Find(txtUserName.Text, txtPassword.Text);

                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("~/admin/MainMenu.aspx");
                }
                else
                {
                    lblStatus.Text = "Invalid username or password.";

                }
            //}
            //catch (System.Exception)
            //{
            //    Response.Redirect("/MainMenu.aspx");
            //}
        }
Example #17
0
 public static void SignIn(UserManager manager, ApplicationUser user, bool isPersistent)
 {
     IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
     authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
     authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
Example #18
0
    protected void btnSignIn_OnClick(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.
            ConnectionStrings["GarageDBConnectionString"].ConnectionString;

        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        //to retrieve a user from the database
        var user = manager.Find(txtUserName.Text, txtPassword.Text);

        if (user != null)
        {
            //Call OWIN functionality
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            //Sign in user
            authenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = false
            }, userIdentity);

            //Redirect user to homepage
            Response.Redirect("~/Index.aspx");
        }
        else
        {
            litStatus.Text = "Invalid username or password";
        }
    }
Example #19
0
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.ConnectionStrings["CANDZOILPDBConnectionString"].ConnectionString;

        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        var user = manager.Find(txtUserName.Text, txtPassword.Text);

        if (user != null) {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties{
                IsPersistent = false
            }, userIdentity);

            Response.Redirect("~/Index.aspx");
        }else{
            litStatus.Text = "Invalid username or password";
        }
    }
Example #20
0
        protected void SignIn(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var user = userManager.Find(UserName.Text, Password.Text);
            try
            {
                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("admin/main-menu.aspx");
                }
                else
                {
                    StatusText.Text = "Invalid username or password.";
                    LoginStatus.Visible = true;
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
Example #21
0
        public void SignIn(IAccount account, Boolean createPersistentCookie)
        {
            UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.context));
            ApplicationUser user = manager.FindByName(account.AccountName);

            this.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
            this.AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = createPersistentCookie }, identity);
        }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        // Default UserStore constructor uses the default connection string named: DefaultConnection
        var userStore = new UserStore<IdentityUser>();

        //Set ConnectionString to GarageConnectionString
        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.ConnectionStrings["GarageConnectionString"].ConnectionString;
        var manager = new UserManager<IdentityUser>(userStore);

        //Create new user and try to store in DB.
        var user = new IdentityUser { UserName = txtUserName.Text };

        if (txtPassword.Text == txtConfirmPassword.Text)
        {
            try
            {
                IdentityResult result = manager.Create(user, txtPassword.Text);
                if (result.Succeeded)
                {
                    UserDetail userDetail = new UserDetail
                    {
                        Address = txtAddress.Text,
                        FirstName = txtFirstName.Text,
                        LastName = txtLastName.Text,
                        Guid = user.Id,
                        PostalCode = Convert.ToInt32(txtPostalCode.Text)
                    };

                    UserDetailModel model = new UserDetailModel();
                    model.InsertUserDetail(userDetail);

                    //Store user in DB
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    //If succeedeed, log in the new user and set a cookie and redirect to homepage
                    authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);
                    Response.Redirect("~/Index.aspx");
                }
                else
                {
                    litStatusMessage.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                litStatusMessage.Text = ex.ToString();
            }
        }
        else
        {
            litStatusMessage.Text = "Passwords must match!";
        }
    }
 protected void btnIngresar_Click(object sender, EventArgs e)
 {
     var userStore = new UserStore<IdentityUser>();
     var userManager = new UserManager<IdentityUser>(userStore);
     var user = userManager.Find(txtNombreUsuario.Text, txtContrasenna.Text);
     if (user != null)
     {
         var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
         var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
         authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
         Response.Redirect("~/wfrmInicio.aspx");
     }
 }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.ConnectionStrings["CANDZOILPDBConnectionString"].ConnectionString;
        
        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        IdentityUser user = new IdentityUser();
        user.UserName = txtUserName.Text;

        if (txtPassword.Text.Equals(txtConfirmPassword.Text))
        {
            try {
                IdentityResult result = manager.Create(user, txtPassword.Text);
                if (result.Succeeded)
                {
                    UserInformation info = new UserInformation
                    {

                        Address = txtAddress.Text,
                        FirstName = txtFirstName.Text,
                        LastName = txtLastName.Text,
                        PostalCode = Convert.ToInt32(txtPostalCode.Text),
                        GUID = user.Id
                    };

                    UserInfoModel model = new UserInfoModel();
                    model.InsertUserInformation(info);

                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);
                    Response.Redirect("~/Index.aspx");
                }else{
                    litStatusMessage.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (Exception er)
            {
                litStatusMessage.Text = er.ToString();
            }
        }
        else {
            litStatusMessage.Text = "Passwords must match!";
        }
    }
Example #25
0
 protected void CreateUser_Click(object sender, EventArgs e)
 {
     var userStore = new UserStore<IdentityUser>();
     var manager = new UserManager<IdentityUser>(userStore);
     var user = new IdentityUser() { UserName = UserName.Text };
     IdentityResult result = manager.Create(user, Password.Text);
     if (result.Succeeded) {
         var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
         var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
         authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
         Response.Redirect("~/Login.aspx");
     } else {
         StatusMessage.Text = result.Errors.FirstOrDefault();
     }
 }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                // Default UserStore constructor uses the default connection string named: DefaultConnection
                var userStore = new UserStore<IdentityUser>();
                var manager = new UserManager<IdentityUser>(userStore);

                var user = new IdentityUser() { UserName = txtUsername.Text };
                IdentityResult result = manager.Create(user, txtPassword.Text);

                if (result.Succeeded)
                {
                    //lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
                    //lblStatus.CssClass = "label label-success";

                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);

                    //add user to users db
                    using (DefaultConnectionEF db = new DefaultConnectionEF())
                    {

                        blogUser u = new blogUser();
                        u.userID = user.Id;
                        u.fName = txtFName.Text;
                        u.lName = txtLName.Text;

                        db.blogUsers.Add(u);
                        db.SaveChanges();

                    }
                    //redirect to main menu
                    Response.Redirect("/admin/bibleMenu.aspx");
                }
                else
                {
                    //lblStatus.Text = result.Errors.FirstOrDefault();
                    //lblStatus.CssClass = "label label-danger";
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/errors.aspx");
            }
        }
Example #27
0
        public void HandleCommand(UserManager<User> userManager, string message, Player player)
        {
            try
            {
                string commandText = message.Split(' ')[0];
                message = message.Replace(commandText, "").Trim();
                commandText = commandText.Replace("/", "").Replace(".", "");

                string[] arguments = message.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                foreach (var handlerEntry in _pluginCommands)
                {
                    CommandAttribute commandAttribute = handlerEntry.Value;
                    if (!commandText.Equals(commandAttribute.Command, StringComparison.InvariantCultureIgnoreCase)) continue;

                    MethodInfo method = handlerEntry.Key;
                    if (method == null) return;

                    var authorizationAttributes = method.GetCustomAttributes<AuthorizeAttribute>(true);
                    foreach (AuthorizeAttribute authorizationAttribute in authorizationAttributes)
                    {
                        User user = userManager.FindByName(player.Username);
                        var userIdentity = userManager.CreateIdentity(user, "none");
                        if (!authorizationAttribute.OnAuthorization(new GenericPrincipal(userIdentity, new string[0])))
                        {
                            player.SendMessage("You are not permitted to use this command!");
                            return;
                        }
                    }

                    var e = new HandleCommandActionEventArgs { RoleRequired = commandAttribute.RoleRequired, player = player, command = commandAttribute.Command };
                    HandleCommandAction(this, e);
                    if (e.Cancel)
                    {
                        player.SendMessage("[Perm] §cУ вас нет прав!");
                        return;
                    }

                    if (ExecuteCommand(method, player, arguments)) return;
                }
            }
            catch (Exception ex)
            {
                Log.Warn(ex);
            }
        }
Example #28
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            // Default UserStore constructor uses the default connection string named: DefaultConnection
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);
            var user = new IdentityUser() { UserName = UserName.Text, Email = Email.Text };

            IdentityResult result = manager.Create(user, Password.Text);

            if (result.Succeeded)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);

                //insert necessary info into the user info table
                //make a connection to the database
            string cs = "Data Source=pbc2o8qyql.database.windows.net;Initial Catalog=Hero;User ID=apple;Password=skull!1223";
            SqlConnection conn = new SqlConnection(cs);

              //insert into database for new user. [hash the password?]
                SqlCommand insert = new SqlCommand("insert into [UserInfo]([Username], [Hero Gender], [Level], [currExp]) values(@username,@herogender, @level, @currExp)", conn);
                insert.Parameters.AddWithValue("@username", UserName.Text);
                insert.Parameters.AddWithValue("@herogender", gender.SelectedValue);
                insert.Parameters.AddWithValue("@level", "1");
                insert.Parameters.AddWithValue("@currExp", "0");
                try
                {
                    conn.Open();
                    insert.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    conn.Close();
                }

                Response.Redirect("~/Login.aspx");

            }
            else
            {
                StatusMessage.Text = result.Errors.FirstOrDefault();
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                // Default UserStore constructor uses the default connection string named: DefaultConnection
                var userStore = new UserStore<IdentityUser>();
                var manager = new UserManager<IdentityUser>(userStore);

                //create a new user using the selected fields
                var user = new IdentityUser() {
                    UserName = txtUsername.Text,
                    Email = txtEmail.Text,
                    //FirstName = txtFirstName.Text,
                    //LastName = txtLastName.Text,
                    //Address = txtAddress.Text,
                    //PostalCode = txtPostalCode.Text,
                    //Province = ddlProvince.SelectedValue
                };

                //create a user with the current details
                IdentityResult result = manager.Create(user, txtPassword.Text);

                //if creation was a success
                if (result.Succeeded)
                {
                    //lblStatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);

                    //sign the user in and redirect to products page
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                    Response.Redirect("/admin/products.aspx", false);
                }
                else
                {
                    //if failed, display an error message
                    lblStatusMessage.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/Error.aspx?Message=" + ex.Message);
            }
        }
        public HttpResponseMessage Post([FromBody]LoginPasswordUser user)
        {
            // Default UserStore constructor uses the default connection string named: DefaultConnection
            var userStore = new UserStore<IdentityUser>();

            //Set ConnectionString to GarageConnectionString
            userStore.Context.Database.Connection.ConnectionString =
                System.Configuration.ConfigurationManager.ConnectionStrings["GarageConnectionString"].ConnectionString;
            var manager = new UserManager<IdentityUser>(userStore);

            //Create new user and try to store in DB.
            var iUser = new IdentityUser { UserName = user.login };

            IdentityResult result = manager.Create(iUser, user.password);
            if (result.Succeeded)
            {
                UserDetail userDetail = new UserDetail
                {
                    Address = user.user.Address,
                    FirstName = user.user.FirstName,
                    LastName = user.user.LastName,
                    Guid = iUser.Id,
                    PostalCode = Convert.ToInt32(user.user.PostalCode)
                };

                UserDetailFacade facade = new UserDetailFacade(db);
                facade.Insert(userDetail);

                //Store identity-user in DB
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(iUser, DefaultAuthenticationTypes.ApplicationCookie);

                //If succeedeed, log in the new user and set the cookie
                authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "Bruger er registreret");
                return response;
            }
            else
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, result.Errors.ToString());
                return response;
            }
        }