You should look into building your own membership provider, and override the ValidateUser() function to check the member's credentials against your external system. Doing so also makes it possible to use the standard asp.net login controls in your umbraco website.
Depending on what other needs you have (eg getting member info, ...), you could inherit from the umbraco membership provider or inherit from the MembershipProvider class.
Be aware that when using your own membership provider, your "members" section may become unusable!
Don't forget to change the membership provider settings in web.config to use your own provider instead of umbraco's
It worked, on the login page, now I can login using the ExternalSystem authentication.
However, when I go to the protected page, it is always still showing the login form, even though I am already logged in.
This is my class
public class MyMembershipProvider: umbraco.providers.members.UmbracoMembershipProvider { public override bool ValidateUser(string username, string password) {
> a few props on your provider shouldn't be there as you've built your own (eg defaultMemberTypeAlias is umbraco specific)
How do you delete properties...? But it doesn't really matter if we leave it, does it?
The problem is that when I log in, I still can't view the protected page, so I think I need to do one of these 2:
Assign the user to a group/role after they login. But how and where (in what class/function) do you do it?
Protect a page in such a way so that it can be accessed if someone is logged in (regardless whether they are in a group/role). Is it even possible?
I think your 4th point is trying to address this issue, but how do you roll your own RoleProvider and set the roles when protecting pages?? Do you inherit some class and override some method, or...?
public class MyMembershipProvider: umbraco.providers.members.UmbracoMembershipProvider { public override bool ValidateUser(string username, string password) { if (ExternalSystem.ValidateUser(username, password)) { if (!base.ValidateUser(username, password)) AddEditMemberToUmbraco(username, password); return true; }
return false; }
///
/// Add a Member to Umbraco or update the password if already exists. /// private void AddEditMemberToUmbraco(string username, string password) { const string DEFAULT_MEMBER_GROUP = "Active";
MembershipUser member = Membership.GetUser(username); if (member == null) { member = Membership.CreateUser(username, password, ""); Roles.AddUserToRole(member.UserName, DEFAULT_MEMBER_GROUP); } else // Member already exists, update password member.ChangePassword(member.GetPassword(), password); } }
and in Web.config, change the type of UmbracoMembershipProvider to your class
when creating a member user in umbraco 6, an notification email, which contains username and password should be sent to the member user's email address, after creating member user.
Custom member login
I have some protected pages, where members have to login to view these pages.
The problem is, I cannot use the Umbraco "Members" because I do not have access to the members, which are stored on an external system.
All I can access is this API: OurExternalSystem.LoginIsValid(string username, string password)
Any suggestion how to merge this with Umbraco pages? Thanks in advance.
(I have no problem doing C# coding to manually authenticate as that's what I have been doing before trying out Umbraco)
Hi Hardy,
You should look into building your own membership provider, and override the ValidateUser() function to check the member's credentials against your external system. Doing so also makes it possible to use the standard asp.net login controls in your umbraco website.
Depending on what other needs you have (eg getting member info, ...), you could inherit from the umbraco membership provider or inherit from the MembershipProvider class.
Be aware that when using your own membership provider, your "members" section may become unusable!
Don't forget to change the membership provider settings in web.config to use your own provider instead of umbraco's
PS: Also deleted your second post :D
Hope this helps.
Regards,
/Dirk
Thanks a lot Dirk.
It worked, on the login page, now I can login using the ExternalSystem authentication.
However, when I go to the protected page, it is always still showing the login form, even though I am already logged in.
This is my class
public class MyMembershipProvider: umbraco.providers.members.UmbracoMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
if(OurExternalSystem.LoginIsValid(username, password)
return true;
return base.ValidateUser(username, password);
}
}
And in the web.config
<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="UmbracoMembershipProvider" type="MyMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed"/>
I think I know why the protected page is still showing the login form.
Perhaps this has something to do with member group/role, which doesn't exist. How do we get around this, Dirk? Thank you :)
Hi Hardy,
Hmm, not sure what may be causing this... but here's a few things I'd change...
- your ValidateUser() should only return the return value from OurExternalSystem.LoginIsValid()
- a few props on your provider shouldn't be there as you've built your own (eg defaultMemberTypeAlias is umbraco specific)
- may (as in - not sure if it will make a diff) need to change the name of the provider, something different from UmbracoMembershipProvider
- may need to look into rolling your own RoleProvider to get roles to be set when protecting a specific area of the site.
Hope this gets you closer to the solution.
Btw, also deleted your obsolete posts.
Cheers,
/Dirk
Thanks Dirk
> a few props on your provider shouldn't be there as you've built your own (eg defaultMemberTypeAlias is umbraco specific)
How do you delete properties...?
But it doesn't really matter if we leave it, does it?
The problem is that when I log in, I still can't view the protected page, so I think I need to do one of these 2:
I think your 4th point is trying to address this issue, but how do you roll your own RoleProvider and set the roles when protecting pages??
Do you inherit some class and override some method, or...?
Thanks again,
Hardi
Solution
Create a new class MyMembershipProvider.cs:
using System.Web.Security;
public class MyMembershipProvider: umbraco.providers.members.UmbracoMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
if (ExternalSystem.ValidateUser(username, password))
{
if (!base.ValidateUser(username, password))
AddEditMemberToUmbraco(username, password);
return true;
}
return false;
}
///
/// Add a Member to Umbraco or update the password if already exists.
///
private void AddEditMemberToUmbraco(string username, string password)
{
const string DEFAULT_MEMBER_GROUP = "Active";
MembershipUser member = Membership.GetUser(username);
if (member == null)
{
member = Membership.CreateUser(username, password, "");
Roles.AddUserToRole(member.UserName, DEFAULT_MEMBER_GROUP);
}
else // Member already exists, update password
member.ChangePassword(member.GetPassword(), password);
}
}
and in Web.config, change the type of UmbracoMembershipProvider to your class
<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="UmbracoMembershipProvider" type="MyMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed"/>
Hi,
I have just implemented this solution. (using Umbraco 4.7)
Web.config
<membership defaultProvider="MFBMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" name="MFBMembershipProvider" type="Web.dev.app.MFBMembershipProvider" />
<add enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" />
</providers>
</membership>
public class MFBMembershipProvider : umbraco.providers.members.UmbracoMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
MFBIntegrationServiceClient client = new MFBIntegrationServiceClient();
//validate from the MFB service
AuthenticateContactResult authenticateUserResult = JavaScriptConvert.DeserializeObject<AuthenticateContactResult>(client.AuthenticateContact(username));
//check that the passwords match
//TODO hash the passwords
if (authenticateUserResult._password.Trim() == password.Trim())
{
if (!base.ValidateUser(username, password))
{ AddEditMemberToUmbraco(username, password);
}
return true;
}
else
{
return false;
}
}
// Add a Member to Umbraco or update the password if already exists.
///
private void AddEditMemberToUmbraco(string username, string password)
{
const string DEFAULT_MEMBER_GROUP = "Active";
MembershipUser member =Membership.GetUser(username);
if (member == null)
{
member = Membership.CreateUser(username, password, "");
Roles.AddUserToRole(member.UserName, DEFAULT_MEMBER_GROUP);
}
else // Member already exists, update password
member.ChangePassword(member.GetPassword(), password);
}
}
But I get the following error. Any ideas?
Object reference not set to an instance of an object.
Stack Trace:
Hi ,
when creating a member user in umbraco 6, an notification email, which contains username and password should be sent to the member user's email address, after creating member user.
Is there any way I can do it. Thanks in advance.
Many Thanks
Harish
is working on a reply...