internal bool ValidateUniqueLogin(MemberSave contentItem, MembershipProvider membershipProvider, HttpActionContext actionContext)
{
if (contentItem == null) throw new ArgumentNullException("contentItem");
if (membershipProvider == null) throw new ArgumentNullException("membershipProvider");
int totalRecs;
var existingByName = membershipProvider.FindUsersByName(contentItem.Username.Trim(), 0, int.MaxValue, out totalRecs);
switch (contentItem.Action)
{
case ContentSaveAction.Save:
//ok, we're updating the member, we need to check if they are changing their login and if so, does it exist already ?
if (contentItem.PersistedContent.Username.InvariantEquals(contentItem.Username.Trim()) == false)
{
//they are changing their login name
if (existingByName.Cast<MembershipUser>().Select(x => x.UserName)
.Any(x => x == contentItem.Username.Trim()))
{
//the user cannot use this login
return false;
}
}
break;
case ContentSaveAction.SaveNew:
//check if the user's login already exists
if (existingByName.Cast<MembershipUser>().Select(x => x.UserName)
.Any(x => x == contentItem.Username.Trim()))
{
//the user cannot use this login
return false;
}
break;
default:
//we don't support this for members
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return true;
}