/// <summary> /// Logs the user in or creates the a site user account if the user does not exist, based on membership user. /// Sets the logged user in the session. /// </summary> /// <exception cref="ValidationException"></exception> /// <returns>The user id of the authenticated user</returns> public static int TryFinishMembershipLogin(SessionWrapper session, MembershipUser membershipUser, IUsersService service) { if (membershipUser == null) { session.ClearUser(); FormsAuthentication.SignOut(); throw new ArgumentNullException("Can not finish membership signin with membership not set."); } var siteUser = service.GetByProviderId(AuthenticationProvider.Membership, membershipUser.ProviderUserKey.ToString()); if (siteUser == null) { //User does not exist on Nearforums db siteUser = new User(); siteUser.UserName = membershipUser.UserName; siteUser.Email = membershipUser.Email; siteUser = service.Add(siteUser, AuthenticationProvider.Membership, membershipUser.ProviderUserKey.ToString()); } session.SetUser(siteUser, AuthenticationProvider.Membership); return siteUser.Id; }