SharePoint 2010 在允许匿名访问的网站中隐藏登陆链接
SharePoint 2010 在允许匿名访问的网站中隐藏登陆链接
最近在使用Welcome.ascx用户控件时,发现很多东西都是要靠它来呈现。比如这里,关于在站点中对于匿名访问用户隐藏登陆链接也与它有关。
大概分两个步骤完成这样功能。非常简单。需要用到母版页和SharePoint Application Page link控件。
1. 复制Welcome.ascx控件,命名CustomWelcome.ascx。覆盖OnLoad事件,给匿名用户隐藏登陆应用程序页面链接。
2. 在母版页中引用这个自定义的CustomWelcome.ascx。
在C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES中你会找到Welcome.ascx文件,复制后打开CustomWelcome.ascx文件。你会看到对于验证用户,很多菜单项如我的设置、以其他用户身份登陆、注销等是可用的。在ID“ExplicitLogOut”下所有菜单项是可用的。你可以看到,Personal Actions控件是不可见的,当用户被成功验证后可见。
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Control Language="C#" Inherits="Microsoft.SharePoint.WebControls.Welcome,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" AutoEventWireup="false" compilationMode="Always" %> <SharePoint:PersonalActions accesskey="<%$Resources:wss,personalactions_menu_ak%>" ToolTip="<%$Resources:wss,open_menu%>" runat="server" id="ExplicitLogout" Visible="false"> <CustomTemplate> <SharePoint:FeatureMenuTemplate runat="server" FeatureScope="Site" Location="Microsoft.SharePoint.StandardMenu" GroupId="PersonalActions" id="ID_PersonalActionMenu" UseShortId="true" > <SharePoint:MenuItemTemplate runat="server" id="ID_PersonalInformation" Text="<%$Resources:wss,personalactions_personalinformation%>" Description="<%$Resources:wss,personalactions_personalinformationdescription%>" MenuGroupId="100" Sequence="100" ImageUrl="/_layouts/images/menuprofile.gif" UseShortId="true" /> <SharePoint:MenuItemTemplate runat="server" id="ID_LoginAsDifferentUser" Text="<%$Resources:wss,personalactions_loginasdifferentuser%>" Description="<%$Resources:wss,personalactions_loginasdifferentuserdescription%>" MenuGroupId="200" Sequence="100" UseShortId="true" /> <SharePoint:MenuItemTemplate runat="server" id="ID_RequestAccess" Text="<%$Resources:wss,personalactions_requestaccess%>" Description="<%$Resources:wss,personalactions_requestaccessdescription%>" MenuGroupId="200" UseShortId="true" Sequence="200" /> <SharePoint:MenuItemTemplate runat="server" id="ID_Logout" Text="<%$Resources:wss,personalactions_logout%>" Description="<%$Resources:wss,personalactions_logoutdescription%>" MenuGroupId="200" Sequence="300" UseShortId="true" /> <SharePoint:MenuItemTemplate runat="server" id="ID_PersonalizePage" Text="<%$Resources:wss,personalactions_personalizepage%>" Description="<%$Resources:wss,personalactions_personalizepagedescription%>" ImageUrl="/_layouts/images/menupersonalize.gif" ClientOnClickScript="javascript:ChangeLayoutMode(true);" PermissionsString="AddDelPrivateWebParts,UpdatePersonalWebParts" PermissionMode="Any" MenuGroupId="300" Sequence="100" UseShortId="true" /> <SharePoint:MenuItemTemplate runat="server" id="ID_SwitchView" MenuGroupId="300" Sequence="200" UseShortId="true" /> <SharePoint:MenuItemTemplate runat="server" id="MSOMenu_RestoreDefaults" Text="<%$Resources:wss,personalactions_restorepagedefaults%>" Description="<%$Resources:wss,personalactions_restorepagedefaultsdescription%>" ClientOnClickNavigateUrl="javascript:SP.SOD.execute('browserScript', 'MSOWebPartPage_RestorePageDefault')" MenuGroupId="300" Sequence="300" UseShortId="true" /> </SharePoint:FeatureMenuTemplate> </CustomTemplate> </SharePoint:PersonalActions>另一部分是作为SharePoint Application Page Link的用户控件ExplicitLogin。
<SharePoint:ApplicationPageLink runat="server" id="ExplicitLogin" ApplicationPageFileName="Authenticate.aspx" AppendCurrentPageUrl=true Text="<%$Resources:wss,login_pagetitle%>" style="display:none" Visible="false" />这个链接我们需要研究一下。默认是不可见的;当用户未被认证时出现。也就是匿名用户看到的。这样他们就可以登陆了。
我们在CustomWelcome.ascx控件中加入脚本,覆盖OnLoad事件,为非验证用户隐藏ExplicitLogin链接。
protected override void OnLoad(EventArgs e) { //base.OnLoad(e); base.OnLoad(e); if (HttpContext.Current.User.Identity.IsAuthenticated) { this.ExplicitLogout.Visible = true; } else { this.ExplicitLogin.Visible = false; this.ExplicitLogin.Attributes.CssStyle.Add("display", "block"); } }在母版页引用这个自定义的CustomWelcome.ascx文件。
<%@ Register TagPrefix="wssuc" TagName="CustomWelcome" src="~/_controltemplates/CustomWelcome.ascx" %>
保存。重新进入网站,就会看到,对于未验证用户,登陆链接已经看不到了。
但是我发现,左上角的选项卡异常。
找到母版页中语句:
<wssuc:Welcome id="IdWelcome" runat="server" EnableViewState="false"></wssuc:Welcome>添加Visible="False"后,刷新首页。恢复正常。
如果你看懂了Welcome.ascx文件及其结构,你会发现自定义变得容易而且有趣。