1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Role provider

Role provider

4 posts, 0 answered
  1. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    04 Apr 2011
    Link to this post
    Hello Telerik,
    can you plese provide me a sample working with custom membership provider? or at least telling me wich methods should I reimplement?

    Thanks
    Paolo
  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    16 Aug 2017
    05 Apr 2011
    Link to this post
    Hi Paolo,

    Can you, please, check this forum thread which is about a discussion on Sitefinity membership provider model and contains explanations and code samples about implementing a custom membership provider.
    If you need any additional information, please do not hesitate to contact me.

    Best wishes,
    Boyan Barnev
    the Telerik team

  3. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    05 Apr 2011
    Link to this post
    Hello Boyan,
    for what concern the Membership provider I should be ok (I can authenticate my users)
    I've got really big and urgent problems (that needs to be solved asap) with the role provider.......

    Here's my file :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Telerik.Sitefinity.Security.Data;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Security;
    using System.Configuration.Provider;
    using Telerik.Sitefinity.Localization;
    using Telerik.Sitefinity.Security.Model;
      
    namespace SitefinityWebApp
    {
        public class IFRoleProvider : RoleDataProvider
        {
            private IQueryable<UserLink> ListUserLinks = null;
            public IFRoleProvider()
            {
            }
      
            public override Telerik.Sitefinity.Security.Model.Role CreateRole(Guid id, string roleName)
            {
                if (id == Guid.Empty)
                    throw new ArgumentNullException("id");
      
                if (!String.IsNullOrEmpty(roleName))
                {
                    if (this.RoleExists(roleName))
                        throw new ProviderException(Res.Get<ErrorMessages>().RoleAlreadyExists.Arrange(roleName));
                }
      
                var role = new Role();
      
                //Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
      
                //role.Id = id;
                //role.Name = roleName;
                //role.LastModified = DateTime.Now;
      
                //role.ApplicationName = base.ApplicationName;
      
                //RoleManager.GetManager().SaveChanges();
                return role;
            }
      
            public override Telerik.Sitefinity.Security.Model.Role CreateRole(string roleName)
            {
                throw new NotImplementedException();
            }
      
            public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink(Guid id)
            {
                Telerik.Sitefinity.Security.Model.UserLink link = CreateUserLink();
      
                link.UserId = id;
                return link;
            }
      
            public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink()
            {
                Telerik.Sitefinity.Security.Model.UserLink link = new Telerik.Sitefinity.Security.Model.UserLink();
      
                link.Id = new Guid();
                link.ApplicationName = base.ApplicationName;
      
                return link;
            }
      
            public override void Delete(Telerik.Sitefinity.Security.Model.UserLink item)
            {
                throw new NotImplementedException();
            }
      
            public override void Delete(Telerik.Sitefinity.Security.Model.Role item)
            {
                throw new NotImplementedException();
            }
      
            public override Telerik.Sitefinity.Security.Model.Role GetRole(Guid id)
            {
                using (IDEAEntities2 entities = new IDEAEntities2())
                {
                    Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
      
                    var profilo = entities.SF_PROFILI.Where(a1 => a1.SFID == id).FirstOrDefault();
      
                    if (profilo != null)
                    {
                        role.ApplicationName = base.ApplicationName;
                        role.Id = profilo.SFID;
                        role.Name = profilo.DESCR;
      
                        //  role.LastModified = DateTime.MinValue;
                    }
      
                    return role;
                }
            }
      
            public override IQueryable<Telerik.Sitefinity.Security.Model.Role> GetRoles()
            {
                List<Telerik.Sitefinity.Security.Model.Role> lst = new List<Role>();
      
                using (IDEAEntities2 entities = new IDEAEntities2())
                {
                    var profili = entities.SF_PROFILI;
      
                    foreach (var p in profili)
                    {
                        Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
      
                        role.Name = p.DESCR;
                        Guid g = p.SFID;
                        role.Id = g;
      
                        //role = GetRole(p.SFID);
      
                        lst.Add(role);
                    }
                }
      
                return lst.AsQueryable();
            }
      
            public override Telerik.Sitefinity.Security.Model.UserLink GetUserLink(Guid id)
            {
                Telerik.Sitefinity.Security.Model.UserLink userLink = new Telerik.Sitefinity.Security.Model.UserLink();
      
                using (IDEAEntities2 entities = new IDEAEntities2())
                {
                    var sf_UserLink = entities.SF_PROFILI_UTENTI.Where(o1 => o1.GUID_LINK == id).FirstOrDefault();
      
                    if (sf_UserLink != null)
                    {
                        userLink.Id = sf_UserLink.GUID_LINK;
                        userLink.UserId = sf_UserLink.GUID_USER;
      
                        userLink.ApplicationName = base.ApplicationName;
      
                        userLink.Role = GetRoleFromUserLink(id);
      
                        //var mi = new ManagerInfo();
      
                        //mi.ApplicationName = base.ApplicationName;
                        //mi.ManagerType = typeof(UserManager).FullName;
                        //mi.ProviderName = "IFRole";
      
                        //userLink.MembershipManagerInfo = mi;
      
                        return userLink;
                    }
                    else
                        return null;
                }
            }
      
            public override IQueryable<Telerik.Sitefinity.Security.Model.UserLink> GetUserLinks()
            {
                if (ListUserLinks != null)
                //if (false)
                    return ListUserLinks;
                else
                {
                    List<Telerik.Sitefinity.Security.Model.UserLink> lst = new List<Telerik.Sitefinity.Security.Model.UserLink>();
      
                    using (IDEAEntities2 entities = new IDEAEntities2())
                    {
                        var linkProfili = entities.I_UTENTI.Where(o1 => o1.STATO == 0).Select(o1 => o1.SF_PROFILI_UTENTI);
      
                        foreach (var i in linkProfili)
                        {
                            Telerik.Sitefinity.Security.Model.UserLink userLink = GetUserLink(i.GUID_LINK);
      
                            lst.Add(userLink);
                        }
                    }
      
                    ListUserLinks = lst.AsQueryable();
                    return ListUserLinks;
                }
            }
      
            private Telerik.Sitefinity.Security.Model.Role GetRoleFromUserLink(Guid userLinkID)
            {
                using (IDEAEntities2 entities = new IDEAEntities2())
                {
                    var userlink = entities.SF_PROFILI_UTENTI.Where(o => o.GUID_LINK == userLinkID).FirstOrDefault();
      
                    if (userlink == null)
                    {
                        return null;
                    }
      
                    Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
      
                    role.ApplicationName = base.ApplicationName;
                    role.Id = userlink.ID_PROFILO.Value;
                    role.Name = userlink.SF_PROFILI.DESCR;
      
                    return role;
                }
            }
      
                }
    }

    I can't have it working properly ... when I go to the role section in the backoffice I got a "Object not set to an instance of an object" while loading the list of users associated with the role....

    can you provide me a custom role provider implementation?
    Thanks
  4. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    16 Aug 2017
    05 Apr 2011
    Link to this post
    Hi Paolo,

    Please check this forum thread where Ivan has given a detailed explanation and example of a custom role provider, and tell me if the information helps.

    Greetings,
    Boyan Barnev
    the Telerik team

4 posts, 0 answered