Pages

Tuesday, September 21, 2010

Redirect user to a different page when they Login via Form Based Authentication (FBA) in SharePoint 2007

Hello Friends,

We have an application in which we have 3 UserTypes/Roles. Uers are reside under those any one of the UserTypes
When User login, depends on their usertype/role we should redirect them to their homepage.
User are going to Login inside the application via Form Based Authentication.
What steps we need to do to customize login page and make fulfil of our requirement?
Let us look at step by step of our requirement.
1>     Under Layouts Folder, create a new folder give meaningful name like your application name, let us say for example “ApplicationSecurity
2>     The location of the original FBA Login.aspx page is here:  Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS” folder.
3>     Copy Login.aspx under the newly created folder “ApplicationSecurity”. Now we have Login.aspx page is here:Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\ApplicationSecurity\Login.aspx
Next, open the web.config for your SharePoint application from the Virtual Directories, find the <forms loginUrl element, and change it from:
    <authentication mode="Forms">
      <
forms loginUrl="/_layouts/login.aspx" />
    </
authentication>
to:
    <authentication mode="Forms">
      <
forms loginUrl="/_layouts/ApplicationSecurity/Login.aspx" />
    </
authentication>
Create a new Class Library project in Visual Studio and add references to Microsoft.SharePoint.dll, Microsoft.SharePoint.ApplicationPages.dll, and System.Web.
Rename Class1.cs to Login.cs.
using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.Security;namespace SPFBASolutions.Login
{
   
public class Login : Microsoft.SharePoint.ApplicationPages.LoginPage    {
       
protected override void OnLoad(EventArgs e)
        {
           
base.OnLoad(e);
           
this.login.LoggedIn += new EventHandler(OnLoggedIn);
        }

       
// Fires after user has sucessfully logged in        void OnLoggedIn(object sender, EventArgs e)
        {
           
// Get the user            MembershipUser Logginuser = Membership.GetUser(this.login.UserName);

           
if (Logginuser!= null)
            {
               
//Custom Actions code
            }
        }
    }
}
Next, open the Custom Login.aspx page in Visual Studio which resides under this location Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\ApplicationSecurity\ and edit its Assembly and Page declarations to point the new  code behind assembly.
<%@ Assembly Name=" SPFBASolutions.Login,Version=1.0.0.0, Culture=neutral, PublicKeyToken=a9ddedr5gacy9r0bn"%>
<%@ Page Language="C#" Inherits=" SPFBASolutions.Login.Login" MasterPageFile="~/_layouts/custom.master"%>
Last Step, place the compiled assembly into GAC.
Disha Shah

“Access Denied” Error in Sharepoint 2007

Hello Friends

“Access Denied” Error in Sharepoint 2007 :

In an application , when I save some of items under users who are belong to a Group which has Read Permission they get the error “Access Denied” when they add, edit or update any List item. Understood!!!

Then I write the code which do the higher operations against their permissions to under the “System Account” privilegaes  by running code inside this code block SPSecurity.RunWithElevatedPrivileges(delegate() ..
Like

SPSecurity.RunWithElevatedPrivileges(delegate()
{
     SPContext.Current.Web.AllowUnsafeUpdates = true;
     //take the lists and make update operation
     SPContext.Current.Web.Update();
}

Still giving me same error “Access Denied”!!!!  A big frustration!!

But after digging I get what we should be under the SPSecurity.RunWithElevatedPrivileges(delegate().

Under SPSecurity.RunWithElevatedPrivileges(delegate() we should never take an object of SPcontext.current.Web

SPSecurity.RunWithElevatedPrivileges(delegate()
{
                   
      using (SPSite oSite = new SPSite(SPContext.Current.Web.Site.Url))
      {
      // creating a new SPSite running under Application pool idenity
                        
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                oWeb.AllowUnsafeUpdates = true;

                  // Code
            }

It runs like a charm!!!
                               
Attention:

Not assign any variable by using SPContext.Current.Web rather than use adminWeb for example take list like that inside the block SPSecurity.RunWithElevatedPrivileges(delegate().

Disha Shah

Monday, September 20, 2010