2

I have one solution in SharePoint 2013 in which FBA is enable to login user in the system.

I want the FBA user to change the password whenever he/she logs into the system the first time.

I have created web part to change password in separate page. And to check the current user's password change status I am thinking to place one web part in the master page and write code in to which will redirect to Change Password page if one has not changed password yet.

My code is as per below:

SPUser currentUser = SPContext.Current.Web.CurrentUser;
if (SPSecurity.AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms)
{
    // running the code with elevated privilages
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(currentUser.LoginName);
        if (user != null)
        {
            if (user.LastPasswordChangedDate == user.CreationDate)
            {

            }
        }
    });
}

I referred this from this link. But I am not getting MembershipUser.

I am getting an error

The method or operation is not implemented.

when control comes to the statement System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(currentUser.LoginName);

And the condition if (SPSecurity.AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms) is evaluated to true even if I log in with Windows Authenticated user.

What should I do? Is there any other better way to accomplish this?

2

Finally found answer by googling for hours.

Tons of thanks to the owner of this post.

I just need to write below line of code to determine the current user's authentication type whether it is FBA or Windows.

string info = null;
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"))
    {
        // code to redirect the user to change password page.
    }
}

For getting better idea the post mentioned in the answer conveys more clarity.

Thanks,

Hope this helps others too!!!

  • Can you please add your steps with complete code?? – Kamalpreet Oct 1 '15 at 3:25
0

Here is my solution, since Membership.GetUser throws a not implemented exception:

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.