Simple example with Custom Membership provider
Hello,
I need to write my own custom membership provider that only needs to replace the ValidateUser method.... I tried using a
TestProvider : MembershipProvider
TestProvider : SitefinityMembershipProvider
Hello Paolo,
Please take a look at this post
www.sitefinity.com/.../extending-membership-provider.aspx
Best wishes,
Ivan Dimitrov
the Telerik team
Hello Ivan,
I just looked at that posted before, my question is from which class should I hinerits ? I'll use my custom SQLServer database to validate the user.....so I think I can't use the OpenAccessMembershipProvider, am I right?
Hello Paolo,
MembershipDataProvider is the base class. There is OpenAccessMembershipProvider which inherits from MembershipDataProvider. OpenAccessMembershipProvider is our implementation related to the OA model. If you are not going to use our logic and our database as a storage you can inherit from MembershipDataProvider
Best wishes,
Ivan Dimitrov
the Telerik team
Hello Ivan,
thanks a lot, just a question, when I login/logout it executes the
public override IQueryable<
Telerik.Sitefinity.Security.Model.User
> GetUsers()
throw new System.NotImplementedException();
Hello Paolo,
In your previous post you said that you would use another database to get the users. In this method you should query this external for Sitefinity database.
If the user belong to another database and it is not created through Sitefinity the Validate method should also be rewritten, because it will not work outside of the OA provider.
Kind regards,
Ivan Dimitrov
the Telerik team
Hello Ivan,
but the GetUsers method should retrieve all the user that are inside my external DB? I've done a simple test doing :
public override IQueryable<
Telerik.Sitefinity.Security.Model.User
> GetUsers()
IQueryable<
Telerik.Sitefinity.Security.Model.User
> lst = new List<
Telerik.Sitefinity.Security.Model.User
>().AsQueryable();
return lst;
public override bool ValidateUser(string userName, string password)
return true;
int a = 0;
re
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
|
Hi Paolo,
Yes you need to query your database in GetUsers method and return a Queriable list of yuor users. Currently you are returning an empty list.Also you should set the login form control to use your custom membership provider. If this is the backend login control you should see a drop down with the two providers inside it.
Kind regards,
Ivan Dimitrov
the Telerik team
Hello Ivan,
I don't know if I'm doing something wrong or if it's a bug..... by the way If I do so :
public override IQueryable<
Telerik.Sitefinity.Security.Model.User
> GetUsers()
Stopwatch sw = new Stopwatch();
sw.Start();
List<
Telerik.Sitefinity.Security.Model.User
> lst = new List<
Telerik.Sitefinity.Security.Model.User
>();
//using (XXXEntities entities = new XXXEntities())
//
// Parallel.ForEach(entities.I_UTENTI, (i) =>
//
// User user = new User();
// user.SetUserName(i.UTENTE.ToUpper());
// lst.Add(user);
// );
//
//sw.Stop();
//TimeSpan ts = sw.Elapsed;
return lst.AsQueryable();
MembershipManagerInfo and ApplicationName, what should I put in there?
Thanks
Paolo
Hi Paolo,
You should create a user object and return a list of users
User newUser =
new
User ;
newUser.Comment =
""
;
newUser.LastActivityDate = DateTime.UtcNow.AddDays(-1);
newUser.LastModified = DateTime.MinValue;
newUser.FailedPasswordAnswerAttemptWindowStart = DateTime.MinValue;
newUser.FailedPasswordAttemptWindowStart = DateTime.MinValue;
newUser.LastActivityDate = DateTime.MinValue;
newUser.LastLoginDate = DateTime.MinValue;
var mi =
new
ManagerInfo();
newUser.SetCreationDate(DateTime.UtcNow);
newUser.SetLastPasswordChangedDate(DateTime.UtcNow);
newUser.SetLastLockoutDate(DateTime.UtcNow);
mi.ApplicationName = newUser.ApplicationName;
mi.ManagerType =
typeof
(UserManager).FullName;
mi.ProviderName =
"SampleProvider"
;
newUser.ManagerInfo = mi;
users.Add(newUser);
Hello Ivan,
I tried but I always get
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
|
Hello Ivan,
can you please update me on this?
Thanks
Hello Paolo,
This error is thrown when the user does not exist in the datasource. So, the provider name of you use is not correct
var membershipProviderName = "YOUR PROVIDER NAME";
var manager = UserManager.GetManager(membershipProviderName);
manager.Provider.SuppressSecurityChecks = true;
if (manager.ValidateUser(userName, password))
user = manager.GetUser(userName);
if (user == null)
throw new ArgumentException("Invalid user name.");
Can you run the code below to see whether it will pass. The user should not be null if the provider is correct.
Best wishes,
Ivan Dimitrov
the Telerik team
I'm to try this now but my get users will return just a dummy username, am I right? as you pasted me below..... or for each user I've in my collection I need to create such an object?
For my provider name you intend the name of my class?
Thanks
Hello Paolo,
Since you have a custom provider this provider should have a name which you used to register it. Inside manager.ValidateUser you should pass the name and password of the user you are trying to authenticate. This should be an object of type Telerik.Sitefinity.Security.Model.User
Best wishes,
Ivan Dimitrov
the Telerik team
Hello Ivan,
I've returned working on this.... I've got my own MembershipProvider and I used successfully to log in, I've tried going in the backend and changing the page permission (I wish to set it to authenticated), when I set it I got a strange problem.....I got the following error :
A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("IModel", "FK_SF_PROFILI_UTENTI_I_UTENTI", "SF_PROFILI_UTENTI")]
public SF_PROFILI_UTENTI SF_PROFILI_UTENTI
get
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<
SF_PROFILI_UTENTI
>("IModel.FK_SF_PROFILI_UTENTI_I_UTENTI", "SF_PROFILI_UTENTI").Value;
set
((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<
SF_PROFILI_UTENTI
>("IModel.FK_SF_PROFILI_UTENTI_I_UTENTI", "SF_PROFILI_UTENTI").Value = value;
public class IFRoleProvider : RoleDataProvider
public IFRoleProvider()
int t = 0;
public override Telerik.Sitefinity.Security.Model.Role CreateRole(Guid id, string roleName)
throw new NotImplementedException();
public override Telerik.Sitefinity.Security.Model.Role CreateRole(string roleName)
throw new NotImplementedException();
public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink(Guid id)
throw new NotImplementedException();
public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink()
throw new NotImplementedException();
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)
throw new NotImplementedException();
public override IQueryable<
Telerik.Sitefinity.Security.Model.Role
> GetRoles()
throw new NotImplementedException();
public override Telerik.Sitefinity.Security.Model.UserLink GetUserLink(Guid id)
throw new NotImplementedException();
public override IQueryable<
Telerik.Sitefinity.Security.Model.UserLink
> GetUserLinks()
List<
Telerik.Sitefinity.Security.Model.UserLink
> lst = new List<
Telerik.Sitefinity.Security.Model.UserLink
>();
var l = lst.AsQueryable();
return l;
Hi Paolo,
Are you using the Microsoft Entiry Framework? It seems to me that the exception comes from it somewhere. We are soon releasing the providers updates so you could simply use our implementation, because there are some specifics that we have tracked and debugged.
Greetings,Hello Ivan,
yes I'm using EF but I've refactored the tables and now it works... I got problems now with showing the roles in the Administration -> Roles but I've opened another thread
Thanks
Hello Ivan,
when I implement the Membership provider you suggested me to specify a
User newUser = GeneraUtenteSF_Da_UtenteIDEA(user);
newUser.Comment = string.Empty;
newUser.LastActivityDate = DateTime.UtcNow.AddDays(-1);
newUser.LastModified = DateTime.MinValue;
newUser.FailedPasswordAnswerAttemptWindowStart = DateTime.MinValue;
newUser.FailedPasswordAttemptWindowStart = DateTime.MinValue;
newUser.LastActivityDate = DateTime.MinValue;
newUser.LastLoginDate = DateTime.MinValue;
newUser.SetCreationDate(DateTime.UtcNow);
newUser.SetLastPasswordChangedDate(DateTime.UtcNow);
newUser.SetLastLockoutDate(DateTime.UtcNow);
newUser.Id = id;
var mi = new ManagerInfo();
mi.ApplicationName = newUser.ApplicationName;
mi.ManagerType = typeof(UserManager).FullName;
mi.ProviderName = "IFProvider";
Hi Paolo,
You have to set the ApplicationName for the role object.
public override IQueryable<Role> GetRoles()
var allRoles = this.roleProvider.GetAllRoles();
var result = new List<RoleWrapper>();
for (int i = 0; i < allRoles.Length; i++)
var newRole = new RoleWrapper(this.id , allRoles[i]);
newRole.ApplicationName = this.ApplicationName;
result.Add(newRole);
return result.AsQueryable<Role>();
public class RoleWrapper : Telerik.Sitefinity.Security.Model.Role
public RoleWrapper(Guid id, string roleName)
this.Id = id;
this.Name = roleName;
Regards,
Ivan Dimitrov
the Telerik team
Hello Ivan, but should I also set a MembershipInfo to the User / UserLink / Role (I've not got the solution right now...so I remember there's also a ManagerInfo as property.... need I to inherit from RoleManager/UserManager?
I've also noticed that if I put some Users in the Membership provider for the GetUsers() I got an "Object not set to an instance..." when I'm in the Admin->Users section....
Thanks