I have two web applications in SharePoint 2010 say http://svt-pc-01:10000
(Team Site) & http://svt-pc-01:11000
(My Site) and it is having FBA configuration.
Scenario:
Step-1: I will open http://svt-pc-01:10000
(Team Site).
Step-2: I will Click on Sign In which will redirect me to my Custom Login Page:
CustomLogin Page Code:
status = SPClaimsUtility.AuthenticateFormsUser(Context.Request.UrlReferrer, txtUserName.Text, txtPassword.Text);
if (status)
{
if (Context.Request.QueryString.Keys.Count > 1 && Context.Request.QueryString["Source"] != null)
{
Response.Redirect(Context.Request.QueryString["Source"].ToString(), false);
}
else
{
Response.Redirect(currentWebUrl, false);
}
}
Step-3: Now I am successfully logged into "http://svt-pc-01:10000"
Step-4: Now I click on My Site Button which will redirect me to "http://svt-pc-01:11000"
Step-5: By Clicking that button it will call a mediator page Authentication.aspx which is used to set the token for site "http://svt-pc-01:11000"
to achieve single sign on.
Authentication.aspx Code:
SPWeb web;
SPUser user;
string password = "password1";
protected void Page_Load(object sender, EventArgs e)
{
web = SPContext.Current.Web;
user = web.CurrentUser;
SecurityToken token = null;
if (null != (token = GetSecurityToken(user.Email)))
{
EstablishSessionWithToken(token);
if (web.Url == ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString())
{
Response.Redirect(ConfigurationManager.AppSettings["MSIMainSiteUrl"].ToString());
}
else
{
Response.Redirect(ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString());
}
}
base.RedirectToSuccessUrl();
}
private void EstablishSessionWithToken(SecurityToken securityToken)
{
if (null == securityToken)
{
throw new ArgumentNullException("securityToken");
}
Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule fam = Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule.Current;
if (null == fam)
{
throw new ArgumentException(null, "FederationAuthenticationModule");
}
fam.SetPrincipalAndWriteSessionToken(securityToken);
}
private SPIisSettings IisSettings
{
get
{
SPWebApplication webApp;
if (web.Url == ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString())
{
webApp = SPWebApplication.Lookup(new Uri(ConfigurationManager.AppSettings["MSIMainSiteUrl"].ToString()));
}
else
{
webApp = SPWebApplication.Lookup(new Uri(ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString()));
}
SPIisSettings settings = webApp.IisSettings[SPUrlZone.Default];
return settings;
}
}
private SecurityToken GetSecurityToken(string username)
{
SecurityToken token = null;
SPIisSettings iisSettings = IisSettings;
Uri appliesTo;
if (web.Url == ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString())
{
appliesTo = new Uri(ConfigurationManager.AppSettings["MSIMainSiteUrl"].ToString());
}
else
{
appliesTo = new Uri(ConfigurationManager.AppSettings["MSIMyLockerUrl"].ToString());
}
SPFormsAuthenticationProvider authProvider = iisSettings.FormsClaimsAuthenticationProvider;
token = SPSecurityContext.SecurityTokenForFormsAuthentication(
appliesTo,
authProvider.MembershipProvider,
authProvider.RoleProvider,
user.Email,
password);
return token;
}
}
}
(Here I have hard coded the password for user)
Step-6: Now what happens is that it redirects me to http://svt-pc-01:11000
with user logged in. (Sumtimes it doesnt redirect me and throws login page instead. Works randomly don't know why.)
Step-7: Suppose this time I am succesfully logged in. Now I click on "Home" to get back to http://svt-pc-01:10000
.
Step-8: Again this will call the mediator page Authentication.aspx to set token on Team Site http://svt-pc-01:10000
.
Step-9: What happens now is that I am redirected to "http://svt-pc-01:10000"
but user doesnt show logged in. FedAuth Cookie is set then also.(I also observed here that when I reset IIS and refresh the same page it shows user logged in.)
Summary:
I login into TEAM Site with Custom Login Page and then I click on MYSITE and it calls Authentication page to set token for MYSITE. Sometimes it logs me in and sometimes it throws login page.
Also when I am successfully logged into MYSITE when i come back to TEAM site with same mechanism of setting token it doesn't show user logged in.
One more thing I observed is that on IISRESET the same page shows me logged in user on Team Site when I come back from MYSITE.
I am facing this issue since long and unable to resolve it. Can you please help me with it? Thanks a lot.