Exemplo n.º 1
0
        public ActionResult Login()
        {
            try
            {
                GetUserProfileObject PostDataArrived = CS.GetPostData <GetUserProfileObject>(this.Request);

                ConnectionStringSettings connectionStringSetting = ConfigurationManager.ConnectionStrings["ISTATWebClientSSO"];
                if (connectionStringSetting == null || string.IsNullOrEmpty(connectionStringSetting.ConnectionString))
                {
                    throw new Exception("ConnectionString not set");
                }

                if (PostDataArrived.IsSuperAdmin)
                {
                    UserRolesEnum ruolo = UserRolesEnum.Administrator;
                    PostDataArrived.UserRole = new UserRoleObject()
                    {
                        RoleId = (int)ruolo, Role = ruolo.ToString()
                    };
                }
                else
                {
                    connectionStringSetting = ConfigurationManager.ConnectionStrings["ISTATWebClientConnection"];
                    ProfileWidget pw = new ProfileWidget(connectionStringSetting.ConnectionString);
                    PostDataArrived.UserRole = pw.GetRole(PostDataArrived);
                }

                Session[ProfileSession] = PostDataArrived;
                return(CS.ReturnForJQuery(new JavaScriptSerializer().Serialize(PostDataArrived)));
            }
            catch (Exception ex)
            {
                return(CS.ReturnForJQuery(ex.Message));
            }
        }
        public override async Task SetUserClaims(string email, UserRolesEnum userRole, string propCode, string unitNumber)
        {
            var user = await _userManager.FindByEmailAsync(email);

            var roleCheck = await _roleManager.RoleExistsAsync(userRole.ToString());

            if (!roleCheck)
            {
                //create the roles and seed them to the database
                var roleResult = await _roleManager.CreateAsync(new IdentityRole(userRole.ToString()));
            }
            await _userManager.RemoveFromRoleAsync(user, UserRolesEnum.Anonymous.ToString());

            await _userManager.AddToRoleAsync(user, userRole.ToString());

            await _securitySignInService.SignInAsync(user, isPersistent : false);

            switch (userRole)
            {
            case UserRolesEnum.Anonymous:
                break;

            case UserRolesEnum.Tenant:
                await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim(SecurityClaims.PropertyCode.ToString(), propCode));

                await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim(SecurityClaims.UnitNumber.ToString(), unitNumber));

                await _userManager.UpdateAsync(user);

                break;

            case UserRolesEnum.Superintendent:
                await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim(SecurityClaims.PropertyCode.ToString(), propCode));

                await _userManager.UpdateAsync(user);

                break;

            case UserRolesEnum.Administrator:
                break;

            case UserRolesEnum.Worker:
                break;
            }
        }
Exemplo n.º 3
0
 public bool RemoveUserFromRole(string userId, UserRolesEnum roleName)
 {
     if (UserRepository.IsUserADemoUser(userId))
     {
         throw new ArgumentException("You can't change the roles of a demo user");
     }
     IdentityResult result = UserManager.RemoveFromRole(userId, roleName.ToString());
     return result.Succeeded;
 }
Exemplo n.º 4
0
        public List<ApplicationUser> UsersNotInRole(UserRolesEnum roleName)
        {
            List<ApplicationUser> resultList = new List<ApplicationUser>();
            List<ApplicationUser> list = UserManager.Users.ToList();

            foreach (ApplicationUser user in list)
            {
                if (!IsUserInRole(user.Id, roleName.ToString()))
                {
                    resultList.Add(user);
                }
            }

            return resultList;
        }
Exemplo n.º 5
0
        public List<ApplicationUser> UsersInRole(UserRolesEnum roleName)
        {
            List<ApplicationUser> resultList = new List<ApplicationUser>();
            List<ApplicationUser> list = UserManager.Users.ToList();

            foreach (ApplicationUser user in list)
            {
                IList<string> currentRoleList = UserManager.GetRoles(user.Id);
                if (IsUserInRole(user.Id, roleName.ToString()))
                {
                    resultList.Add(user);
                }
            }

            return resultList;
        }
        public UserRoleObject GetRole(GetUserProfileObject PostDataArrived)
        {
            try
            {
                if (PostDataArrived == null || string.IsNullOrEmpty(PostDataArrived.UserCode))
                {
                    throw new Exception("Input Error");
                }

                string sqlquery = string.Format("Select * from UserRoles where UserCode='{0}'", PostDataArrived.UserCode.Replace("'", "''"));

                Sqlconn.Open();
                try
                {
                    DataTable dtres = new DataTable();
                    using (SqlCommand comm = new SqlCommand(sqlquery, Sqlconn))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            da.Fill(dtres);
                        }
                    }
                    UserRolesEnum ruolo = UserRolesEnum.User;
                    if (dtres != null && dtres.Rows.Count > 0)
                    {
                        int RoleCode = Convert.ToInt32(dtres.Rows[0]["RoleId"].ToString());
                        ruolo = (UserRolesEnum)RoleCode;
                    }

                    return(new UserRoleObject()
                    {
                        RoleId = (int)ruolo, Role = ruolo.ToString()
                    });
                }
                catch (Exception) { throw; }
                finally
                {
                    Sqlconn.Close();
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
                throw new Exception(string.Format(ErrorOccuredMess, ex.Message));
            }
        }
        public override async Task SetUserClaims(string email, UserRolesEnum userRole, string propCode, string unitNumber)
        {
            B2CCustomAttributeHelper helper = new B2CCustomAttributeHelper(_config.B2cExtensionAppClientId);
            string userRoleAttributeName    = helper.GetCompleteAttributeName("userrole");
            string propCodeAttributeName    = helper.GetCompleteAttributeName("PropertyCode");


            IDictionary <string, object> extensionInstance = new Dictionary <string, object>();

            extensionInstance.Add(helper.GetCompleteAttributeName("userrole"), userRole.ToString( ));
            if (!string.IsNullOrEmpty(propCode))
            {
                extensionInstance.Add(helper.GetCompleteAttributeName("PropertyCode"), propCode);
            }
            if (!string.IsNullOrEmpty(unitNumber))
            {
                extensionInstance.Add(helper.GetCompleteAttributeName("UnitNumber"), unitNumber);
            }

            var userToUpdate = new User
            {
                AdditionalData = extensionInstance
            };

            var result = await _graphClient.Users
                         .Request()
                         .Filter($"identities/any(c:c/issuerAssignedId eq '{IntLoggedUser.Login}' and c/issuer eq '{_config.TenantId}')")
                         .Select(e => new
            {
                e.DisplayName,
                e.Id,
                e.Identities
            })
                         .GetAsync();

            var user = result.CurrentPage.FirstOrDefault();

            ObjectIdentity objIdentity = user?.Identities.FirstOrDefault();

            if (objIdentity != null && objIdentity.IssuerAssignedId == IntLoggedUser.Login)
            {
                await _graphClient.Users[user.Id]
                .Request()
                .UpdateAsync(userToUpdate);
            }
        }
Exemplo n.º 8
0
 public bool IsUserInRole(string userId, UserRolesEnum roleName) => UserManager.IsInRole(userId, roleName.ToString());
        public List <GetUserProfileObject> GetUserList(string SingleSignOnConf)
        {
            List <GetUserProfileObject> utentiSSON = GetSingleSignONUsers(SingleSignOnConf);

            try
            {
                string sqlquery = string.Format("Select * from UserRoles");
                Sqlconn.Open();
                try
                {
                    DataTable dtres = new DataTable();
                    using (SqlCommand comm = new SqlCommand(sqlquery, Sqlconn))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            da.Fill(dtres);
                        }
                    }

                    foreach (DataRow userrow in dtres.Rows)
                    {
                        GetUserProfileObject user = utentiSSON.Find(u => u.UserCode == userrow["UserCode"].ToString());
                        if (user == null)
                        {
                            DeleteUserforSynk(userrow["UserCode"].ToString());
                            continue;
                        }
                        UserRolesEnum ruolo = UserRolesEnum.User;
                        if (user.IsSuperAdmin)
                        {
                            ruolo = UserRolesEnum.Administrator;
                        }
                        else
                        {
                            ruolo = (UserRolesEnum)Convert.ToInt32(userrow["RoleId"].ToString());
                        }

                        user.UserRole = new UserRoleObject()
                        {
                            RoleId = (int)ruolo, Role = ruolo.ToString()
                        };
                    }
                    utentiSSON.FindAll(u => u.UserRole == null).ForEach(u =>
                    {
                        UserRolesEnum ruolo = UserRolesEnum.User;
                        if (u.IsSuperAdmin)
                        {
                            ruolo = UserRolesEnum.Administrator;
                        }
                        u.UserRole = new UserRoleObject()
                        {
                            RoleId = (int)ruolo, Role = ruolo.ToString()
                        };
                    });

                    return(utentiSSON);
                }
                catch (Exception) { throw; }
                finally
                {
                    Sqlconn.Close();
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
                throw new Exception(string.Format(ErrorOccuredMess, ex.Message));
            }
        }