2 // System.Web.UI.WebControls.LoginStatus class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Globalization;
34 using System.Security.Permissions;
35 using System.Web.Security;
37 namespace System.Web.UI.WebControls {
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [DefaultEvent ("LoggingOut")]
44 [Designer ("System.Web.UI.Design.WebControls.LoginStatusDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45 public class LoginStatus : CompositeControl {
47 private static readonly object loggedOutEvent = new object ();
48 private static readonly object loggingOutEvent = new object ();
50 private LinkButton logoutLinkButton;
51 private ImageButton logoutImageButton;
52 private LinkButton loginLinkButton;
53 private ImageButton loginImageButton;
62 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
64 public virtual string LoginImageUrl {
66 object o = ViewState ["LoginImageUrl"];
67 return (o == null) ? String.Empty : (string) o;
71 ViewState.Remove ("LoginImageUrl");
73 ViewState ["LoginImageUrl"] = value;
78 public virtual string LoginText {
80 object o = ViewState ["LoginText"];
81 return (o == null) ? Locale.GetText ("Login") : (string) o;
85 ViewState.Remove ("LoginText");
87 ViewState ["LoginText"] = value;
91 [DefaultValue (LogoutAction.Refresh)]
93 public virtual LogoutAction LogoutAction {
95 object o = ViewState ["LogoutAction"];
96 return (o == null) ? LogoutAction.Refresh : (LogoutAction) o;
99 if ((value < LogoutAction.Refresh) || (value > LogoutAction.RedirectToLoginPage))
100 throw new ArgumentOutOfRangeException ("LogoutAction");
101 ViewState ["LogoutAction"] = (int) value;
106 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
108 public virtual string LogoutImageUrl {
110 object o = ViewState ["LogoutImageUrl"];
111 return (o == null) ? String.Empty : (string) o;
115 ViewState.Remove ("LogoutImageUrl");
117 ViewState ["LogoutImageUrl"] = value;
123 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
126 public virtual string LogoutPageUrl {
128 object o = ViewState ["LogoutPageUrl"];
129 return (o == null) ? String.Empty : (string) o;
133 ViewState.Remove ("LogoutPageUrl");
135 ViewState ["LogoutPageUrl"] = value;
140 public virtual string LogoutText {
142 object o = ViewState ["LogoutText"];
143 return (o == null) ? Locale.GetText ("Logout") : (string) o;
147 ViewState.Remove ("LogoutText");
149 ViewState ["LogoutText"] = value;
153 protected override HtmlTextWriterTag TagKey {
154 get { return HtmlTextWriterTag.A; }
160 protected internal override void CreateChildControls ()
164 // we create controls for all possibilities
165 logoutLinkButton = new LinkButton ();
166 logoutLinkButton.Command += new CommandEventHandler (LogoutClick);
167 logoutImageButton = new ImageButton ();
168 logoutImageButton.Command += new CommandEventHandler (LogoutClick);
169 loginLinkButton = new LinkButton ();
170 loginLinkButton.Command += new CommandEventHandler (LoginClick);
171 loginImageButton = new ImageButton ();
172 loginImageButton.Command += new CommandEventHandler (LoginClick);
174 // adds controls at the end (after setting their properties)
175 Controls.Add (logoutLinkButton);
176 Controls.Add (logoutImageButton);
177 Controls.Add (loginLinkButton);
178 Controls.Add (loginImageButton);
182 protected virtual void OnLoggedOut (EventArgs e)
184 // this gets called only if the authentication was successful
185 EventHandler loggedOut = (EventHandler) Events [loggedOutEvent];
186 if (loggedOut != null)
190 protected virtual void OnLoggingOut (LoginCancelEventArgs e)
192 // this gets called before OnAuthenticate so we can abort the authentication process
193 LoginCancelEventHandler loggingOut = (LoginCancelEventHandler) Events [loggingOutEvent];
194 if (loggingOut != null)
195 loggingOut (this, e);
198 protected internal override void OnPreRender (EventArgs e)
200 base.OnPreRender (e);
201 // documentation says we select Login*|Logout* here
202 // but tests shows that the selection is done even
203 // if OnPreRender is never called
206 protected internal override void Render (HtmlTextWriter writer)
211 RenderContents (writer);
214 protected internal override void RenderContents (HtmlTextWriter writer)
219 EnsureChildControls ();
221 bool authenticated = false;
223 Page.VerifyRenderingInServerForm (this);
224 authenticated = Page.Request.IsAuthenticated;
227 bool logoutImage = (LogoutImageUrl.Length > 0);
228 logoutLinkButton.Visible = authenticated && !logoutImage;
229 logoutImageButton.Visible = authenticated && logoutImage;
231 bool loginImage = (LoginImageUrl.Length > 0);
232 loginLinkButton.Visible = !authenticated && !loginImage;
233 loginImageButton.Visible = !authenticated && loginImage;
235 if (logoutLinkButton.Visible) {
236 logoutLinkButton.Text = LogoutText;
237 logoutLinkButton.Render (writer);
238 } else if (logoutImageButton.Visible) {
239 logoutImageButton.AlternateText = LogoutText;
240 logoutImageButton.ImageUrl = LogoutImageUrl;
241 writer.AddAttribute(HtmlTextWriterAttribute.Name, logoutImageButton.UniqueID);
242 logoutImageButton.Render (writer);
243 } else if (loginLinkButton.Visible) {
244 loginLinkButton.Text = LoginText;
245 loginLinkButton.Render (writer);
246 } else if (loginImageButton.Visible) {
247 loginImageButton.AlternateText = LoginText;
248 loginImageButton.ImageUrl = LoginImageUrl;
249 writer.AddAttribute(HtmlTextWriterAttribute.Name, loginImageButton.UniqueID);
250 loginImageButton.Render (writer);
254 [MonoTODO ("for design-time usage - no more details available")]
255 protected override void SetDesignModeState (IDictionary data)
257 base.SetDesignModeState (data);
262 public event EventHandler LoggedOut {
263 add { Events.AddHandler (loggedOutEvent, value); }
264 remove { Events.RemoveHandler (loggedOutEvent, value); }
267 public event LoginCancelEventHandler LoggingOut {
268 add { Events.AddHandler (loggingOutEvent, value); }
269 remove { Events.RemoveHandler (loggingOutEvent, value); }
274 private void LogoutClick (object sender, CommandEventArgs e)
276 LoginCancelEventArgs lcea = new LoginCancelEventArgs (false);
281 FormsAuthentication.SignOut ();
283 switch (LogoutAction) {
284 case LogoutAction.Refresh:
285 HttpContext.Current.Response.Redirect (Page.Request.Url.AbsoluteUri);
287 case LogoutAction.RedirectToLoginPage:
288 FormsAuthentication.RedirectToLoginPage ();
290 case LogoutAction.Redirect:
291 string url = LogoutPageUrl;
293 url = Page.Request.Url.AbsoluteUri;
294 HttpContext.Current.Response.Redirect (url);
301 private void LoginClick (object sender, CommandEventArgs e)
303 FormsAuthentication.RedirectToLoginPage ();