Exemple #1
0
        public int Insert(UserItem userItem, IOwinContext owinContext, HttpRequestBase Request)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    InstanceUserManager instanceUserManager = owinContext.GetUserManager <InstanceUserManager>();

                    var user = new IdentityUserItem
                    {
                        UserName  = userItem.UserName,
                        Email     = userItem.Email,
                        FirstName = userItem.FirstName,
                        LastName  = userItem.LastName,
                    };

                    IdentityResult result = instanceUserManager.Create(user, userItem.Password);

                    if (result.Succeeded)
                    {
                        foreach (string item in userItem.UserItemSelectedRoles)
                        {
                            var roles = context.Roles.ToList();

                            if (roles.Count() > 0)
                            {
                                foreach (IdentityRole identityRole in roles)
                                {
                                    if (identityRole.Name == item)
                                    {
                                        instanceUserManager.AddToRole(user.Id, identityRole.Name);
                                    }
                                }
                                ;
                            }
                        }

                        string code        = instanceUserManager.GenerateEmailConfirmationToken(user.Id);
                        string callbackUrl = new UrlHelper(Request.RequestContext).Action("ConfirmEmail", "Login", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        instanceUserManager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking this link " + callbackUrl);
                        scope.Complete();
                        return(1);
                    }
                    else
                    {
                        userItem.UserItemRoles = context.Roles.Select(x => new SelectListItem {
                            Text = x.Name, Value = x.Id.ToString()
                        }).ToList();
                        foreach (var error in result.Errors)
                        {
                            strErrorMessage = ErrorMessage + " " + error;
                        }
                        scope.Dispose();
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    serviceException       = ex;
                    userItem.UserItemRoles = context.Roles.Select(x => new SelectListItem {
                        Text = x.Name, Value = x.Id.ToString()
                    }).ToList();
                    if (ex.Message != null)
                    {
                        strErrorMessage = ex.Message;
                    }
                    else
                    {
                        strErrorMessage = ex.InnerException.Message;
                    }
                    scope.Dispose();
                    return(-1);
                }
            }
        }
Exemple #2
0
        public int Update(UpdateUserItem updateUserItem, IOwinContext owinContext)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    InstanceUserManager instanceUserManager = owinContext.GetUserManager <InstanceUserManager>();

                    IdentityUserItem identityUserItem = instanceUserManager.FindById(updateUserItem.Id.ToString());

                    if (updateUserItem != null)
                    {
                        identityUserItem.Email     = updateUserItem.Email;
                        identityUserItem.UserName  = updateUserItem.UserName;
                        identityUserItem.FirstName = updateUserItem.FirstName;
                        identityUserItem.LastName  = updateUserItem.LastName;

                        IdentityResult result = instanceUserManager.Update(identityUserItem);

                        if (result.Succeeded)
                        {
                            //this part is not similar to create need more investigation
                            foreach (string item in updateUserItem.UserItemSelectedRoles)
                            {
                                string roleName = context.Roles.Find(item).Name; //sometimes this part not works, may be the context is not initialized?
                                instanceUserManager.AddToRole(identityUserItem.Id, roleName);
                            }

                            List <IdentityUserRole> listIdentityUserRole = instanceUserManager.FindById(identityUserItem.Id).Roles.ToList();

                            foreach (IdentityUserRole item in listIdentityUserRole)
                            {
                                if (!updateUserItem.UserItemSelectedRoles.Contains(item.RoleId))
                                {
                                    string roleToRemove = context.Roles.Find(item.RoleId).Name;
                                    instanceUserManager.RemoveFromRole(identityUserItem.Id, roleToRemove);
                                }
                            }
                            scope.Complete();
                            return(1);
                        }
                        else
                        {
                            updateUserItem.UserItemRoles = context.Roles.Select(x => new SelectListItem {
                                Text = x.Name, Value = x.Id.ToString()
                            }).ToList();
                            foreach (var error in result.Errors)
                            {
                                strErrorMessage = ErrorMessage + " " + error;
                            }
                            scope.Dispose();
                            return(0);
                        }
                    }
                    else
                    {
                        updateUserItem.UserItemRoles = context.Roles.Select(x => new SelectListItem {
                            Text = x.Name, Value = x.Id.ToString()
                        }).ToList();
                        strErrorMessage = "There is no user with provided ID";
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    serviceException             = ex;
                    updateUserItem.UserItemRoles = context.Roles.Select(x => new SelectListItem {
                        Text = x.Name, Value = x.Id.ToString()
                    }).ToList();
                    if (ex.Message != null)
                    {
                        strErrorMessage = ex.Message;
                    }
                    else
                    {
                        strErrorMessage = ex.InnerException.Message;
                    }
                    scope.Dispose();
                    return(-1);
                }
            }
        }