示例#1
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            // Default UserStore constructor uses the default connection string named: DefaultConnection
            var userStore = new UserStore();
            var user      = new IdentityUser()
            {
                UserName = UserName.Text
            };

            user.PasswordHash  = MySecurityProvider.PlainToSHA256(Password.Text);
            user.SecurityStamp = System.DateTime.Now.Ticks.ToString();
            string result = userStore.CreateAsync(user).Result;

            if (result == "OK")
            {
                StatusMessage.Text = string.Format("Utente {0} è stato correttamente creato!", user.UserName);
            }
            else
            {
                StatusMessage.Text = "Utente non creato";
            }
        }
示例#2
0
        public HttpResponseMessage RegisterUser(FormDataCollection formsValues)
        {
            var        userStore     = new UserStore();
            UsersModel model         = new UsersModel();
            var        userName      = formsValues["UserName"];
            var        password      = formsValues["Password"];
            var        cognome       = formsValues["Cognome"];
            var        nome          = formsValues["Nome"];
            var        role          = formsValues["Role"];
            var        codicefiscale = formsValues["CodiceFiscale"];
            var        user          = new IdentityUser()
            {
                UserName = userName.ToUpper()
            };

            user.PasswordHash  = MySecurityProvider.PlainToSHA256(password);
            user.SecurityStamp = System.DateTime.Now.Ticks.ToString();
            try
            {
                string result = userStore.CreateAsync(user).Result;
                user.Id = userStore.FindByNameAsync(userName.ToUpper()).Result.Id;
                if (result == "OK")
                {
                    BackendUserService bus         = new BackendUserService();
                    BackendUser        userBackend = new BackendUser();
                    userBackend.Cognome       = cognome.Trim().ToUpper();
                    userBackend.Nome          = nome.Trim().ToUpper();
                    userBackend.UserName      = userName.Trim().ToUpper();
                    userBackend.CodiceFiscale = codicefiscale.Trim().ToUpper();
                    userBackend.Domain        = role.ToUpper();
                    userBackend.UserId        = long.Parse(user.Id);
                    bus.Save(userBackend);
                    model.success = "true";
                }
                else
                {
                    model.success = "false";
                    model.message = "Utente non creato";
                }
                var resultRole = (userStore.AddToRoleAsync(user, int.Parse(role.ToUpper()))).Result;
                if (resultRole != 1)
                {
                    model.success = "false";
                    model.message = string.Format("Utente {0} non aggiunto a ruolo {1} è stato correttamente creato!", user.UserName, role);
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(ManagedException))
                {
                    ManagedException mEx = new ManagedException("Errore creazione utente. Dettaglio: " + ex.Message +
                                                                "StackTrace: " + ((ex.StackTrace != null) ? ex.StackTrace.ToString() : " vuoto "),
                                                                "ERR317",
                                                                string.Empty,
                                                                string.Empty,
                                                                ex.InnerException);
                    ErrorLogInfo err = new ErrorLogInfo(mEx);
                    log.Error(err);
                    model.success = "false";
                    model.message = string.Format("Utente {0} non correttamente creato", user.UserName);
                }
                else
                {
                    model.success = "false";
                    model.message = "Utene non creato";
                }
                return(this.Request.CreateResponse <UsersModel>(HttpStatusCode.OK, model));
            }
            return(this.Request.CreateResponse <UsersModel>(HttpStatusCode.OK, model));
        }
示例#3
0
        public HttpResponseMessage UpdateOwnProfile(FormDataCollection formsValues)
        {
            UsersMailModel model         = new UsersMailModel();
            var            userName      = formsValues["UserName"];
            var            password      = formsValues["Password"];
            var            cognome       = formsValues["Cognome"];
            var            nome          = formsValues["Nome"];
            var            domain        = formsValues["Domain"];
            var            codicefiscale = formsValues["CodiceFiscale"];
            var            userStore     = new UserStore();
            string         result        = "OK";

            try
            {
                var user = userStore.FindByNameAsync(userName).Result;
                if (!(string.IsNullOrEmpty(password)))
                {
                    user.PasswordHash  = MySecurityProvider.PlainToSHA256(password);
                    user.SecurityStamp = System.DateTime.Now.Ticks.ToString();
                    result             = userStore.UpdateAsync(user).Result;
                }
                if (result == "OK")
                {
                    BackendUserService bus         = new BackendUserService();
                    BackendUser        userBackend = new BackendUser();
                    userBackend.Cognome       = cognome.Trim().ToUpper();
                    userBackend.Nome          = nome.Trim().ToUpper();
                    userBackend.UserName      = userName.Trim().ToUpper();
                    userBackend.Domain        = domain;
                    userBackend.CodiceFiscale = codicefiscale.Trim().ToUpper();
                    userBackend.UserId        = long.Parse(user.Id);
                    bus.Update(userBackend);
                    model.success = "true";
                }
                else
                {
                    model.success = "false";
                    model.message = "Utente non aggiornato";
                    return(this.Request.CreateResponse <UsersMailModel>(HttpStatusCode.OK, model));
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(ManagedException))
                {
                    ManagedException mEx = new ManagedException("Errore aggiornamento utente. Dettaglio: " + ex.Message +
                                                                "StackTrace: " + ((ex.StackTrace != null) ? ex.StackTrace.ToString() : " vuoto "),
                                                                "ERR322",
                                                                string.Empty,
                                                                string.Empty,
                                                                ex.InnerException);
                    ErrorLogInfo err = new ErrorLogInfo(mEx);
                    log.Error(err);
                    model.success = "false";
                    model.message = string.Format("Utente {0} non correttamente aggiornato", userName);
                    return(this.Request.CreateResponse <UsersMailModel>(HttpStatusCode.OK, model));
                }
                model.success = "false";
                model.message = ex.Message;
                return(this.Request.CreateResponse <UsersMailModel>(HttpStatusCode.OK, model));
            }
            return(this.Request.CreateResponse <UsersMailModel>(HttpStatusCode.OK, model));
        }