让WebPart使用自定义的用户数据

来源:互联网 发布:jk淘宝 编辑:程序博客网 时间:2024/04/30 03:00

一般来说,WebPart使用系统集成的验证 方式,而很多时间我们需要自己的登录机制,那么这个时候,就需要将我们的登陆用户,关联到WebPart的个性化信息设置。

分几个步骤:

1。生成WebPart的个性化数据库,配置个性化数据库连接。参考:http://blog.csdn.net/addyou/archive/2007/06/21/1660387.aspx

2。创建自己的个性数据提供程序

需要创建/重载三个类,如下:

(1) PortalSqlPersonalizationProvider

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace HFWebCtlLib
{
    
public class PortalSqlPersonalizationProvider : SqlPersonalizationProvider
    
{

        
protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob)
        
{
            
base.SavePersonalizationBlob(webPartManager, path, GetLoginUserId(), dataBlob);
        }


        
protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
        
{
            
base.LoadPersonalizationBlobs(webPartManager, path, GetLoginUserId(), ref sharedDataBlob, ref userDataBlob);
        }


        
protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)
        
{
            
base.ResetPersonalizationBlob(webPartManager, path, GetLoginUserId());
        }


        
public override PersonalizationScope DetermineInitialScope(WebPartManager webPartManager, PersonalizationState loadedState)
        
{
            
return webPartManager.Personalization.InitialScope;
        }

        
/// <summary>
        
/// 返回当前登录用户ID
        
/// </summary>
        
/// <returns></returns>

        private string GetLoginUserId()
        
{
            
return System.Web.HttpContext.Current.Session["userid"].ToString();
        }

    }

}

(2) PortalWebPartPersonalization

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Collections;

namespace HFWebCtlLib
{
    
public class PortalWebPartPersonalization : WebPartPersonalization
    
{
        
public PortalWebPartPersonalization(WebPartManager webPartManager) : base(webPartManager)
        
{}

        
protected override System.Collections.IDictionary UserCapabilities
        
{
            
get
            
{
                Hashtable capabilities 
= new Hashtable();
                capabilities.Add(WebPartPersonalization.ModifyStateUserCapability, WebPartPersonalization.ModifyStateUserCapability);

                
return capabilities;
            }

        }

    }

}

(3) PortalWebPartManager ——这是WebPartManager控件的替代,在页面中使用该控件

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace HFWebCtlLib
{
    [DefaultProperty(
"Url")]
    [ToolboxData(
"<{0}:PortalWebPartManager runat=server></{0}:PortalWebPartManager>")]
    
public class PortalWebPartManager : WebPartManager
    
{
        
protected override WebPartPersonalization CreatePersonalization()
        
{
            
return new PortalWebPartPersonalization(this);
        }

    }

}

以上三个类,可以创建在一个单独的类库或者控件库中,最好不要放在项目本身的类里。

4。 修改Web.config文件,确保有以下配置节

 

<configuration>
    
<connectionStrings>
        
<remove name="LocalSqlServer"/>
        
<add name="LocalSqlServer" connectionString="Data Source=10.138.194.69;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
    
</connectionStrings>
    
<system.web>

        
<webParts>
            
<personalization defaultProvider="PortalProvider">
                
<providers>
                    
<add name="PortalProvider" type="HFWebCtlLib.PortalSqlPersonalizationProvider" connectionStringName="LocalSqlServer"/>
                
</providers>
            
</personalization>
        
</webParts>
     
      
</system.web>
</configuration>

 

通过以上步骤,程序在运行时,自session中取登录用户的ID作为个性化的依据,这样就可以实际为系统内的用户提供个性化定制的功能了。

原创粉丝点击