Custom Membership and Role Provider in Sitefinity 7.0
Hi,
I am trying to implement custom membership and role provider for the pages created in sitefinity. I have created the custom membership provider inheriting ASP.NET Membership provider, but i could not see the users from my database under Administrator/Users, when I select the custom provider.
And when I create role provider, the sitefinity user is getting authorized by my role provider. I could not see the roles from my database under Administrator/Roles.
My requirement is I want to authenticate the user from my own provider and provide access to the sitefinity pages based on my own role provider. I could achieve the authentication part, but could not provide permissions based on roles from my provider.
Could you please provide me some source, so that I can implement Custom membership and role provider in my application.
The changes I did in web.config:
<membership defaultProvider="Default" >
<providers>
<add name="Default" type="Telerik.Sitefinity.Security.Data.SitefinityMembershipProvider, Telerik.Sitefinity"/>
<add name="CustomMembershipProvider" type="AssociatePortal.CustomMembershipProvider" connectionStringName="DealerPortalDB" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="AssociatePortal/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="CustomRoleProvider">
<providers>
<add connectionStringName="DealerPortalDB" name="CustomRoleProvider" type="AssociatePortal.CustomRoleProvider"/>
</providers>
</roleManager>
Hello Sravani,
We have a full code sample for implementing the CustomMembershipProvider here. Following the documentation tutorial should lead to a working membership provider, you could check the registration of the provider.
We do not have a documentation with currently available resources for custom Role providers and this is something we'd like to schedule as an addition to our documentation for our future releases. In order to achieve compatibility with the ASP.NET Membership and Role providers, we have implemented wrappers for both. What this means is that you can actually develop your custom Role or Membership provider using entirely .NET Framework standards and API, and register it in your Sitefinity project as you would do in any other Asp.Net WebApplication. More information about the RoleProvider could be found here.
You should implement a set of rules for the Role Provider, similarly to the Membership Provider and add the logic for the roles data there.
/// <summary>
/// Gets the provider abilities for the current principal. E.g. which operations are supported and allowed
/// </summary>
/// <value>The provider abilities.</value>
public
override
ProviderAbilities Abilities
get
ProviderAbilities abilities =
new
ProviderAbilities();
abilities.ProviderName =
this
.Name;
abilities.ProviderType =
this
.GetType().FullName;
abilities.AddAbility(
"GetRole"
,
true
,
true
);
abilities.AddAbility(
"AddRole"
,
true
,
true
);
abilities.AddAbility(
"AssingUserToRole"
,
false
,
false
);
abilities.AddAbility(
"UnAssingUserFromRole"
,
false
,
false
);
abilities.AddAbility(
"DeleteRole"
,
false
,
false
);
return
abilities;
Hi Nikola,
I have implemented the custom membership provider. Its working fine. I am trying to implement RoleDataProvider now. I am unable to get an idea of how to implement CreateUserLinks() method for assigning roles to users and insert a record in my database. Can you please provide the code for that.
And I could not found any method like AssignUserToRole as in ProviderAbilities.
Thanks & Regards,
Sravani
Hi Sravani,
Actually, you should implement GetUsersLinks() and CreateUserLink() or CreateUserLink(Guid id). There us no virtual method CreateUserLinks().
Implementation for the GetUsersLinks()
public
override
IQueryable<Telerik.Sitefinity.Security.Model.UserLink> GetUserLinks()
List<UserLink> listEntities =
new
List<UserLink>();
using
(var entity =
new
Sitefinity63Entities())
var rolesQuery = from ul
in
entity.sf_user_link
where ul.app_name ==
this
.ApplicationName
select
new
UserLink()
ApplicationName = ul.app_name,
Id = ul.id,
LastModified = (DateTime)ul.last_modified,
Role =
new
Role()
Id = entity.sf_roles.Where(r=> r.id == ul.role_id).FirstOrDefault().id,
Name = entity.sf_roles.Where(r=> r.id == ul.role_id).FirstOrDefault().nme,
ApplicationName = entity.sf_roles.Where(r => r.id == ul.role_id).FirstOrDefault().app_name
,
MembershipManagerInfo = UserManagerInfo,
UserId = (Guid)ul.user_id
;
listEntities = rolesQuery.ToList();
return
listEntities.AsQueryable();
Hi Nikola,
Can you tell me how to provide access to the sitefinity backened to edit the pages using custom roles. I have tried many options which are provided in forums but nothing is working in my case.
I have a user which has a custom role. Now I need to provide some permissions to this custom role so that the user can access pages tab with the pages permitted to edit in sitefinity.
Thanks & Regards,
Sravani Shenishetty
Hi
You can set some general permissions from the roles menu for the role you want. Then go to pages back-end page. If you want to set the role global permissions - for all pages, select permissions from the right sidebar and add the role to the actions it should have access to. You can do this for a specific page/group of pages, by adding the role to the page permissions - actions -> permissions. When selecting the role/user to be able to view or manage, in the selector you can choose which provider's role or users to be shown.
Regards,
Nikola Zagorchev
Telerik