Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • hyde 1 post 21 karma points
    Apr 23, 2015 @ 10:53
    hyde
    0

    Umbraco 7 Custom Membership and Role Providers.

    I went through 10's of documentation and I am doing the right thing but I am sure it is a small detail I am missing.

    I want to use my own login mechanism to log in my website. the login mechanism is in an Identity server for single sign on.

    But I want to use the Public access feature in umbraco, so I am adding the roles from my local database. by changing the config file

    and implement the RoleProvider

     class RCSEdRoleProvider : RoleProvider
        {
            private string _ApplicationName = "UmbracoRoleProvider";
            public override void AddUsersToRoles(string[] usernames, string[] roleNames)
            {
            throw new NotImplementedException();
        }
    
    public override string ApplicationName
    {
        get { return _ApplicationName; }
        set
        {
            if(string.IsNullOrEmpty(value))
                throw new ProviderException("ApplicationName Cacnnot be Empty");
    
            if(value.Length > 0x100)
                throw new ProviderException("provider application name too long");
    
            _ApplicationName = value;
        }
    }
    
    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }
    
    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }
    
    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        throw new NotImplementedException();
    }
    
    public override string[] GetAllRoles()
    {
        return UBISRoles.GetRoleList();
    }
    
    public override string[] GetRolesForUser(string username)
    {
        // code to return user role access
        try
        {
           //ool isAuthenticateSession = RCSEd.UolsSecurity.CommonFunctions.CheckAuthenticateSessionOrNot();
    
            if (HttpContext.Current.Session["UOSStudent"] != null)
            {
                return (String[])HttpContext.Current.Session["userRoles"];
            }
            else
            {
                List<String> retval = new List<string>();
                retval.Add("Public");
                return retval.ToArray();
            }
        }
        catch (Exception ex)
        {
           // AppLogWriter _objApplog = new AppLogWriter();
           // _objApplog.WriteLogMessages(ex.Message.ToString());
            //TODO catch Error 
            List<String> retval = new List<string>();
            retval.Add("Public");
            return retval.ToArray();
        }
    
    }
    
    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }
    
    public override bool IsUserInRole(string username, string roleName)
    {
        try
        {
            foreach (String role in (String[])HttpContext.Current.Session["userRoles"])
            {
                if (String.Compare(role, roleName, true) == 0)
                    return true;
            }
    
    
            return false;
    
        }
        catch { }
    
        return false;
    
    }
    
    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }
    
    public override bool RoleExists(string roleName)
    {
    
        foreach (string val in UBISRoles.GetRoleList())
        {
            if (val == roleName)
                return true;
        }
    
        return false;
    }
    

    }

    class UBISRoles
    {
        private static string[] allroles = new string[] { //"Public", 
         "MembershipCandidate",
            "MembershipMember"
          };
    public static string[] GetRoleList()
    {
        return allroles;
    }
    
    
    
    public static void SetRoles(DataTable UBIsRolesResultsTBL)
    {
        List<String> userRoles = new List<string>();
    
        DataRow rec = UBIsRolesResultsTBL.Rows[0];
    
    
        switch (rec["Membership"].ToString())
        {
    
            case "member":
                userRoles.Add("MembershipMember");
                break;
            case "pending member":
                userRoles.Add("MembershipPending");//add
                break;
            case "public":
                userRoles.Add("MembershipPublic");//add
                break;
    
        }
    
    
    
    
    
    
        HttpContext.Current.Session["userRoles"] = userRoles.ToArray();
    
    }
    
    private static void CheckSimpleFieldVal(List<String> userRoles, DataRow rec, string roleName)
    {
        try
        {
            if (string.Compare(rec[roleName].ToString(), "YES", true) == 0)
                userRoles.Add(roleName);
    
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
    
        }
    
    }
    
    private static void CheckSimpleFieldValV2(List<String> userRoles, DataRow rec, string roleName)
    {
        try
        {
            if (string.Compare(rec[roleName].ToString(), "1", true) == 0)
                userRoles.Add(roleName);
    
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
    
        }
    
    }
    

    } And this works fine and everything is being loaded in the member groups in umbraco back office.

    But when I try to implement the MembershipProvider where i feel things is not working. the web config look something like this

    And the Membershiprovider class look like this

    class RCSEdMembershipProvide : MembershipProvider
        {
            string connectionStringName;
            private string _ApplicationName =  "UmbracoMembershipProvider";
            public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
            {
            base.Initialize(name, config);
    
          /*  if (config["ConnectionStringName"] != null)
                connectionStringName = config["connectionStringName"];*/
    
    
    
    
        }
    public override string ApplicationName
    {
        get { return _ApplicationName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ProviderException("ApplicationName Cacnnot be Empty");
    
            if (value.Length > 0x100)
                throw new ProviderException("provider application name too long");
    
            _ApplicationName = value;
        }
    }
    
    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        throw new NotImplementedException();
    }
    
    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
    {
        throw new NotImplementedException();
    }
    
    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        throw new NotImplementedException();
    }
    
    public override bool DeleteUser(string username, bool deleteAllRelatedData)
    {
        throw new NotImplementedException();
    }
    
    public override bool EnablePasswordReset
    {
        get { throw new NotImplementedException(); }
    }
    
    public override bool EnablePasswordRetrieval
    {
        get { throw new NotImplementedException(); }
    }
    
    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }
    
    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }
    
    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }
    
    public override int GetNumberOfUsersOnline()
    {
        throw new NotImplementedException();
    }
    
    public override string GetPassword(string username, string answer)
    {
        throw new NotImplementedException();
    }
    
    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        try
        {
            User user = (User)HttpContext.Current.Session["user"];
            if (user != null)
                return new MembershipUser("RCSEdMembershipProvider", user.DisplayName, username, user.Email, "", "", true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
            else
                return null;
    
        }
        catch
        {
            return null;
        }
    }
    
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        try
        {
            User user = (User)HttpContext.Current.Session["user"];
    
            if (user != null)
                return new MembershipUser("RCSEdMembershipProvider", user.DisplayName, providerUserKey, user.Email, "", "", true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
            else
                return null;
        }
        catch
        {
    
            return null;
        }
    }
    
    public override string GetUserNameByEmail(string email)
    {
        throw new NotImplementedException();
    }
    
    public override int MaxInvalidPasswordAttempts
    {
        get { throw new NotImplementedException(); }
    }
    
    public override int MinRequiredNonAlphanumericCharacters
    {
        get { throw new NotImplementedException(); }
    }
    
    public override int MinRequiredPasswordLength
    {
        get { throw new NotImplementedException(); }
    }
    
    public override int PasswordAttemptWindow
    {
        get { throw new NotImplementedException(); }
    }
    
    public override MembershipPasswordFormat PasswordFormat
    {
        get { throw new NotImplementedException(); }
    }
    
    public override string PasswordStrengthRegularExpression
    {
        get { throw new NotImplementedException(); }
    }
    
    public override bool RequiresQuestionAndAnswer
    {
        get { throw new NotImplementedException(); }
    }
    
    public override bool RequiresUniqueEmail
    {
        get { throw new NotImplementedException(); }
    }
    
    public override string ResetPassword(string username, string answer)
    {
        throw new NotImplementedException();
    }
    
    public override bool UnlockUser(string userName)
    {
        throw new NotImplementedException();
    }
    
    public override void UpdateUser(MembershipUser user)
    {
        throw new NotImplementedException();
    }
    
    public override bool ValidateUser(string username, string password)
    {
    
    
        return true;
    }
    

    } I even put the validateUser as true all the time.

    so when the User log in to my identity server I load the User role in HttpContext.Current.Session["userRoles"] and I limit the public access of my page and redirect the user to the correct pages.

    when the user is not logged the user is redirected to the login page which is fine but when the user is returned and his/her roles is loaded and try to visit the restricted pages the membership provider ValidateUser don't get hit and the User is redirected back to the log in page again and again.

    PS. I am using umbraco 7.1.3

    Excuse the long question but i wanted to cover all my steps. thank you in advance.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jul 02, 2015 @ 14:55
    Dan Diplo
    0

    Did you ever get an answer to this because I'm having the same problem in Umbraco 7.2.6 - it never seems to call the custom role provider when checking access to the page.

Please Sign in or register to post replies

Write your reply to:

Draft