Hi ,
I have created a Custom Membership Provider in SharePoint 2013 for Forms Based Authentication.
Below is the code for CustomMembershipProvider
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Infosys.BE.SessionManagement.DataContract;
using Infosys.BE.SessionManagementService;
using Microsoft.Http;
using System.Runtime.Serialization;
using ColloborationPortal;
namespace SingleSignOnMembershipProvider
{
//Class CMembership is extending SqlMembershipProvider.
public class CMembership : SqlMembershipProvider
{
#region "Variable Declaration"
SqlDataReader ObjSQLReader = null;
SqlConnection ObjSQLConnection = null;
SqlCommand ObjSQLCommand = null;
CustomLog Log = new CustomLog();
#endregion
#region Overridng ValidateUser
public override bool ValidateUser(string userName, string password)
{
return isUserExists(userName);
}
#endregion
#region "Private Methods"
private bool isUserExists(string strUserName)
{
bool IsUserExists = false;
try
{
using (ObjSQLConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringSSO"].ToString()))
{
ObjSQLCommand = new SqlCommand("USP_VALIDATEUSER", ObjSQLConnection);
ObjSQLCommand.CommandType = CommandType.StoredProcedure;
ObjSQLCommand.Parameters.AddWithValue("@USERNAME", strUserName);
ObjSQLConnection.Open();
ObjSQLReader = ObjSQLCommand.ExecuteReader();
while (ObjSQLReader.Read())
{
if (Convert.ToBoolean(ObjSQLReader["ISVALID"]))
{
IsUserExists = true;
}
else
{
IsUserExists = false;
}
}
ObjSQLConnection.Close();
}
}
catch (Exception ex)
{
Log.Tracer("Timestamp: " + DateTime.Now.ToString() + ". Exception: " + ex.Message +
"InnerException: " + ex.InnerException + "Exception Source: " + ex.Source +
"Exception StackTrace: " + ex.StackTrace, "SSOLoginPage");
}
return IsUserExists;
}
}
I am trying to authenticate the user into SharePoint 2013 using the following utility class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.IdentityModel;
using CDPCookie;
namespace ColloborationPortal
{
public class Utility
{
#region "Variable Declaration"
CustomLog Log = new CustomLog();
#endregion
public bool ValidateUser(PortalRequest portalRequest)
{
bool isAuthenticated = false;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(portalRequest.WebApplication + portalRequest.SiteCollectionName))
{
using (SPWeb web = site.OpenWeb())
{
isAuthenticated = SPClaimsUtility.AuthenticateFormsUser((new Uri(web.Url)), PortalRequest.UserName, "pass@word1");
}
}
});
}
catch (Exception ex)
{
Log.Tracer("Timestamp: " + DateTime.Now.ToString() + ". Exception: " + ex.Message +
"InnerException: " + ex.InnerException + "Exception Source: " + ex.Source +
"Exception StackTrace: " + ex.StackTrace, "SSOUtility.cs");
}
return isAuthenticated;
}
}
}
Following the stack trace of the error at the line : isAuthenticated = SPClaimsUtility.AuthenticateFormsUser((new Uri(web.Url)), PortalRequest.UserName, "pass@word1");
===================================================================================
Timestamp: 12/2/2013 3:12:51 PM. Exception: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug>
configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.InnerException: Exception Source: Microsoft.IdentityModelException
StackTrace: at Microsoft.IdentityModel.Protocols.WSTrust.WSTrustChannel.ReadResponse(Message response)
at Microsoft.IdentityModel.Protocols.WSTrust.WSTrustChannel.Issue(RequestSecurityToken rst, RequestSecurityTokenResponse& rstr)
at Microsoft.IdentityModel.Protocols.WSTrust.WSTrustChannel.Issue(RequestSecurityToken rst)
at Microsoft.SharePoint.SPSecurityContext.SecurityTokenForContext(Uri context, Boolean bearerToken, SecurityToken onBehalfOf, SecurityToken actAs, SecurityToken delegateTo, SPRequestSecurityTokenProperties properties)
at Microsoft.SharePoint.SPSecurityContext.SecurityTokenForFormsAuthentication(Uri context, String membershipProviderName, String roleProviderName, String username, String password, SPFormsAuthenticationOption options)
at Microsoft.SharePoint.IdentityModel.SPClaimsUtility.AuthenticateFormsUser(Uri context, String userName, String password)
at ColloborationPortal.Utility.<>c__DisplayClass2.<ValidateUser>b__0()
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass5.<RunWithElevatedPrivileges>b__3()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
at ColloborationPortal.Utility.ValidateUser(PortalRequest portalRequest)
===================================================================================
Please help me out
Regards
Rizwan Shaikh