Monday, February 6, 2012

Custom sign out SharePoint page for FBA user.

Hi, everyone!
In my latest project we are using FBA users for our portal. And in the prototype of the master page such as generic web page we have login control with username, password boxes and ‘sing in’ button. If you will use for login just general sign in control of ASP .NET we will lose with authentication, exception will appear in the page. For fixing this problem we have to use SPClaimsUtility.
For example:
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
    e.Authenticated = 
SPClaimsUtility.AuthenticateFormsUser(Request.Url, signIn.UserName, signIn.Password);
}
But for sign out we have to create custom page for logout current user from portal. In all forums you can find information that’s you can use default signout.aspx page from 14 hive. But it’s true if you created custom login page inherited from SharePoint SignInPage. Default page is working incorrectly, it made sign out but doesn’t remove cookies from browser and the next page what you will see after signout, it will ‘Exception … ArgumentException … encodedValue’.
Insert the next following code to the page for clearing sign out.
protected override void OnLoad(EventArgs e)
{
 base.OnLoad(e);

 FormsAuthentication.SignOut();
 var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        // Clear .ASPAUTH cookie key.
 if (authCookie != null)
 {
  var myCookie = new HttpCookie(authCookie.Name)
               {Expires = DateTime.Now.AddDays(-1)};
  Response.Cookies.Add(myCookie);
 }

 SPIisSettings iisSettingsWithFallback = 
         Site.WebApplication.GetIisSettingsWithFallback(Site.Zone);
 if (iisSettingsWithFallback.UseClaimsAuthentication)
 {
  FederatedAuthentication.SessionAuthenticationModule.SignOut();
                // Clear FedAuth Cookie key
  FederatedAuthentication.SessionAuthenticationModule.DeleteSession                 TokenCookie();
 }


 SPUtility.Redirect(Web.ServerRelativeUrl, SPRedirectFlags.Default,
          Context);
}
That’s all what you need to add to the custom sign out page.

3 comments:

  1. FederatedAuthentication.SessionAuthenticationModule.SignOut() throws a nullReferenceException. Do you know what I can do to prevent this?

    ReplyDelete
  2. FederatedAuthentication.SessionAuthenticationModule.SignOut() throws a nullReferenceException. Do you know what I can do to prevent this?

    ReplyDelete
  3. I were given too much interesting stuff upon your blog. I wager i'm no longer the isolated one having each and the whole one the leisure here! maintain taking place the pleasant war. Top 5 Benefits of Amazon FBA

    ReplyDelete