public bool Evaluate(EvaluationContext evaluationContext, ref object state)
    {
        // will hold the combined roles
        List <string> roles = new List <string>();

        // get the authenticated client identity
        IIdentity client = GetClientIdentity(evaluationContext);

        var config = new NameValueCollection();


        config.Add("applicationName", "/application_name");
        config.Add("connectionStringName", "APPSEC_ASPNET");

        var roleProvider = new CustomRoleProvider();

        roleProvider.Initialize("CustomRoleProvider", config);

        roles.AddRange(roleProvider.GetRolesForUser(client.Name));


        evaluationContext.Properties["Principal"] =
            new UserPrincipal(client, roles.ToArray());


        return(true);
    }
Пример #2
0
 private bool Role(HttpContextBase httpContext)
 {
     if (allowedRoles.Length > 0)
     {
         for (int i = 0; i < allowedRoles.Length; i++)
         {
             if (CustomRoleProvider.GetRolesForUser(unit) == allowedRoles[i])
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        public void TestGetRolesForUserQueryCount()
        {
            var roleProvider = new CustomRoleProvider();
            var queryCount   = new EntityFrameworkActivityLogger();

            using (var usersContext = new ApplicationDbContext())
                using (new WithInterception(queryCount))
                {
                    roleProvider.GetRolesForUser(usersContext, testUserA.UserName);

                    // We expect 1 query to get the user, and 1 query to get the role names
                    Assert.AreEqual(2, queryCount.TotalExecutedCount,
                                    "The query count for CustomRoleProvider::GetRolesForUser exceeded the expected number.");

                    queryCount.Reset();

                    roleProvider.GetRolesForUser(usersContext, testUserB.UserName);

                    // The query count should be the same, regardless of the number of roles a user is a member of
                    Assert.AreEqual(2, queryCount.TotalExecutedCount,
                                    "The query count for CustomRoleProvider::GetRolesForUser exceeded the expected number.");
                }
        }
Пример #4
0
        // GET: Header
        public ActionResult HeaderList()
        {
            string[] role;

            CustomRoleProvider userRole = new CustomRoleProvider();

            role = userRole.GetRolesForUser(User.Identity.Name);

            if (role.Length != 0 && "admin" == role[0])
            {
                ViewBag.onAdmin = true;
            }
            else
            {
                ViewBag.onAdmin = false;
            }

            var list = db.TCategories.ToList();

            return(PartialView("_HeaderList", list));
        }