0

I have a .NET usercontrol running on an Umbraco website, and I'd like to be able to delete the current member from the codebehind (I realise that doesn't sound like a great plan, but this did work on a normal ASP.NET Forms site), but calling System.Web.Security.Membership.DeleteUser(usernameToDelete, trueOrFalse); gives me (on the page I'm redirecting to):

No member with username 'test10@test.test' exists
Exception Details: System.Configuration.Provider.ProviderException: No member with username 'test10@test.test' exists
[ProviderException: No member with username 'test10@test.test' exists]
   umbraco.providers.members.UmbracoProfileProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) +346
   System.Configuration.SettingsBase.SaveCore() +402
   System.Configuration.SettingsBase.Save() +109
   System.Web.Profile.ProfileBase.SaveWithAssert() +31
   System.Web.Profile.ProfileBase.Save() +72
   System.Web.Profile.ProfileModule.OnLeave(Object source, EventArgs eventArgs) +9025062
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Why is it trying to set a property value after I've deleted the user and changed page? I can't see how to set / remove the current Umbraco member.


EDIT: I'm using umbraco v 4.11.1 (Assembly version: 1.0.4715.27659)

Here's the longest version of my logout code that I've tried that still gives the error:

// sort out problems with umbraco caches:
Member.RemoveMemberFromCache(Member.CurrentMemberId());
Member.ClearMemberFromClient(Member.CurrentMemberId());

Roles.RemoveUserFromRole(Page.User.Identity.Name, JobShopRoles.RoleNames.Candidate.ToString());

//logout from: http://stackoverflow.com/questions/412300/formsauthentication-signout-does-not-log-the-user-out
FormsAuthentication.SignOut();
Page.Session.Clear();  // This may not be needed -- but can't hurt
Page.Session.Abandon();

// Clear authentication cookie
HttpCookie rFormsCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
rFormsCookie.Expires = DateTime.Now.AddYears(-1);
Page.Response.Cookies.Add(rFormsCookie);

// Clear session cookie 
HttpCookie rSessionCookie = new HttpCookie("ASP.NET_SessionId", "");
rSessionCookie.Expires = DateTime.Now.AddYears(-1);
Page.Response.Cookies.Add(rSessionCookie);
// Invalidate the Cache on the Client Side

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Page.Response.Cache.SetNoStore();
//END logout

System.Web.Security.Membership.DeleteUser(currentUser.UserName, true);

//TODO: this will consistently give an error trying to update a property on a deleted member. Qs:
//http://stackoverflow.com/questions/14899945/error-trying-to-delete-current-member
//http://our.umbraco.org/forum/core/general/38455-Error-trying-to-delete-current-Member
Response.Redirect("/", false);
Ian Grainger
  • 5,148
  • 3
  • 46
  • 72

1 Answers1

0

What happens if you logout the current user from codebehind before deleting it?

It looks like the deleted user is still logged in and exists in the current session and therefore Umbraco profileprovider tries to do something with it...

EDIT: try these 2 methods after the FormsAuthentication Logout and see if it helps:

Member.RemoveMemberFromCache(Member.CurrentMemberId());
Member.ClearMemberFromClient(Member.CurrentMemberId());

Edit2:

I looked into the UmbracoProfileProvider class to see what is causing the error:

   public override void SetPropertyValues(SettingsContext context,
 SettingsPropertyValueCollection collection) {

                string username = (string)context["UserName"];
                bool authenticated = (bool)context["IsAuthenticated"];

                if (String.IsNullOrEmpty(username) || collection.Count == 0)
                    return;

                Member m = Member.GetMemberFromLoginName(username);
                if (m == null)
                    throw new ProviderException(String.Format("No member with username '{0}' exists", username));


                foreach (SettingsPropertyValue spv in collection) {
                    if (!authenticated && !(bool)spv.Property.Attributes["AllowAnonymous"])
                        continue;

                    if (m.getProperty(spv.Name) != null)
                        m.getProperty(spv.Name).Value = spv.PropertyValue;
                }

            }

You can see your exception message after the provider is trying to retrieve the current Member, based on the username. Either way your context still contains a UserName or the SettingsPropertyValueCollection count is > 0 so it seems your code to logout the member is still leaving something behind.

Try this part instead of all the logout code you have:

   Member.RemoveMemberFromCache(Member.CurrentMemberId());
   Member.ClearMemberFromClient(Member.CurrentMemberId());
   FormsAuthentication.SignOut();
   Roles.DeleteCookie();
   HttpContext.Current.Session.Clear();
   System.Web.Security.Membership.DeleteUser(usernameToDelete, false);

this removes and clears the member from cache and client, signs out, deletes the Role cookie and clears the current session. Finally, after logging out, delete the Member by using its username.

Martijn van der Put
  • 4,062
  • 1
  • 18
  • 26
  • The same thing. I had trouble getting FormsAuthentication.SignOut() to work, but with some help from this fine site I got sign out working by deleting some cookies. I think once the member's been signed out I'll be able to delete them fine, but I'll be abandoning the session, so i cant use that to allow a roundtrip, so that means adding them to a 'deletion queue' or similar :/ – Ian Grainger Feb 15 '13 at 20:03
  • See my edit above, that might help. If not, add the code part where you are handling the logout. – Martijn van der Put Feb 15 '13 at 21:27
  • Again, I found a post which had these two in to solve a different problem - but didn't help. I'll post the longest version of the code I had that still didn't work... – Ian Grainger Feb 16 '13 at 08:24
  • So Roles.DeleteCookie() should fix it? Because I already had the other lines. I'll try it ASAP! – Ian Grainger Feb 16 '13 at 19:17
  • Just tried and same problem. I'll add the version to the question. – Ian Grainger Feb 18 '13 at 09:39
  • I am out of options at this moment. ;) Please post the answer if you have found it. – Martijn van der Put Feb 20 '13 at 09:49