1

I have a Sharepoint site that uses Forms Based Authentication. I would like to discover the last password updated date and the creation, for the purpose of discovering whether they have changed their password since the account was created. I found a question on here already that seems to explain how to do this, but the code is not working for me. Here is a link to that question:

How to force change password on first login for FBA user?

The problem is, I am encountering the same error that the original poster encountered, I am getting an exception "The method or operation is not implemented." on this line: System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(currentUser.LoginName)

Below the original post is another post seeming to indicate how he solved the issue, I tried wrapping my code in that but it still throws the same error. Below is a complete listing of the code for my non visual webpart, can you help me find out how to get the userinfo for the logged in user? I will also accept as an answer, a way to get the userid of the currently logged in user, because with that I can query the database directly to retrieve the information.

I am using Sharepoint2013, VisualStudio2013, and SharePoint 2013 FBA Pack

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;

namespace mysharepointproject.CheckPassword
{
    [ToolboxItemAttribute(false)]
    public class CheckPassword : WebPart
    {
        protected override void CreateChildControls()
        {
            Boolean PasswordChangeRequired = false;
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            if (SPSecurity.AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms)
            {
                SPClaimProviderManager mgr = SPClaimProviderManager.Local;
                if (mgr != null)
                {
                    SPClaim userLogonNameClaim = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName);
                    SPOriginalIssuerType issuerType = SPOriginalIssuers.GetIssuerType(userLogonNameClaim.OriginalIssuer);
                    if (Convert.ToString(issuerType).Equals("Forms"))
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            //The line below fails with The method or operation is not implemented.
                            System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(currentUser.LoginName);
                            if (user != null)
                            {
                                if (user.LastPasswordChangedDate == user.CreationDate)
                                {
                                    PasswordChangeRequired = true;
                                }
                            }
                        });
                    }
                }
            }

            if (PasswordChangeRequired)
            {
                //redirect to the page where users changes their password
                HttpContext.Current.Response.Redirect("~/SitePages/cp.aspx");
            }
        }
    }
}
  • is it not possible to use the current user from SPContext.Web.CurrentUser ? – Gwny Nov 20 '14 at 12:22
  • That's what I tried, but SPContext.Current.Web.CurrentUser.UserID does not actually return the userID that I wanted. I also tried SPContext.Current.Web.CurrentUser.LoginName, but that returned a string that didn't match any of my users in the aspnet db. However, your comment did help me though, I made a visual webpart and included that for debugging purposes, which allowed me to see what was being returned by that loginname. it was what I wanted, just with some extra stuff in front of it. the call to membership.getuser is meant to fix just that, but unfortunately for some reason it failed. – Brian Denomey Nov 20 '14 at 22:17
  • so I guess you're using claims identification? In that case you will get the user-SID instead of the real login name. – Gwny Nov 21 '14 at 7:31
  • Yes, which thankfully i can extract the user name out of with the code below – Brian Denomey Nov 21 '14 at 19:41
0

Here is how I eventually solved the problem:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.SqlClient;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Utilities;

namespace mysharepointproject.CheckPassword
{
    [ToolboxItemAttribute(false)]
    public class CheckPassword : WebPart
    {
        protected override void CreateChildControls()
        {
            Boolean PasswordChangeRequired = false;
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            String username = "";
            if (SPSecurity.AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms)
            {
                SPClaimProviderManager mgr = SPClaimProviderManager.Local;
                if (mgr != null)
                {
                    SPClaim userLogonNameClaim = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName);
                    SPOriginalIssuerType issuerType = SPOriginalIssuers.GetIssuerType(userLogonNameClaim.OriginalIssuer);
                    if (Convert.ToString(issuerType).Equals("Forms"))
                    {
                        //i:0#.w|domain\user for windows
                        //i:0#.f|fbamembershipprovider|username for FBA
                        //so we expect there to be 3 elements in the array.  
                        //if there are more than that, the user has added | into his username, so we'll handle that
                        String[] usernameparts = currentUser.LoginName.ToString().Split('|');
                        if (usernameparts.Length > 2)
                        {
                            for (int i = 2; i < usernameparts.Length; i++)
                            {
                                username += usernameparts[i];
                            }
                            username = username.Replace("%7c", "|");
                        }
                        else
                        {
                            username = usernameparts[usernameparts.Length - 1];
                        }
                    }
                } 
            }

            //connect to db, get user, check state
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source = DOMAIN\\SHAREPOINT; Initial Catalog=ASPNETDB; Integrated Security = SSPI";
            conn.Open();

            SqlCommand sql = new SqlCommand();
            SqlDataReader rdr;
            sql.CommandText = "SELECT 1 as needschanging from dbo.aspnet_Membership M " +
            "Join [aspnet_Users] U on M.[UserId] = U.[UserId] " +
            "Where " +
            "M.[LastPasswordChangedDate] <= M.[CreateDate] " +
            "AND " +
            "U.[UserName] = '" + username + "'";

            sql.Connection = conn;
            rdr = sql.ExecuteReader();

            while (rdr.Read())
            {
                if (rdr["needschanging"].ToString() == "1")
                {
                    PasswordChangeRequired = true;
                }
            }

            conn.Close();

            if (PasswordChangeRequired)
            {
                HttpContext.Current.Response.Redirect("~/SitePages/cp.aspx");
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.