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)]
92 public virtual LogoutAction LogoutAction {
94 object o = ViewState ["LogoutAction"];
95 return (o == null) ? LogoutAction.Refresh : (LogoutAction) o;
98 if ((value < LogoutAction.Refresh) || (value > LogoutAction.RedirectToLoginPage))
99 throw new ArgumentOutOfRangeException ("LogoutAction");
100 ViewState ["LogoutAction"] = (int) value;
105 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
107 public virtual string LogoutImageUrl {
109 object o = ViewState ["LogoutImageUrl"];
110 return (o == null) ? String.Empty : (string) o;
114 ViewState.Remove ("LogoutImageUrl");
116 ViewState ["LogoutImageUrl"] = value;
122 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
124 public virtual string LogoutPageUrl {
126 object o = ViewState ["LogoutPageUrl"];
127 return (o == null) ? String.Empty : (string) o;
131 ViewState.Remove ("LogoutPageUrl");
133 ViewState ["LogoutPageUrl"] = value;
138 public virtual string LogoutText {
140 object o = ViewState ["LogoutText"];
141 return (o == null) ? Locale.GetText ("Logout") : (string) o;
145 ViewState.Remove ("LogoutText");
147 ViewState ["LogoutText"] = value;
151 protected override HtmlTextWriterTag TagKey {
152 get { return HtmlTextWriterTag.A; }
158 protected internal override void CreateChildControls ()
162 // we create controls for all possibilities
163 logoutLinkButton = new LinkButton ();
164 logoutLinkButton.Command += new CommandEventHandler (LogoutClick);
165 logoutImageButton = new ImageButton ();
166 logoutImageButton.Command += new CommandEventHandler (LogoutClick);
167 loginLinkButton = new LinkButton ();
168 loginLinkButton.Command += new CommandEventHandler (LoginClick);
169 loginImageButton = new ImageButton ();
170 loginImageButton.Command += new CommandEventHandler (LoginClick);
172 // adds controls at the end (after setting their properties)
173 Controls.Add (logoutLinkButton);
174 Controls.Add (logoutImageButton);
175 Controls.Add (loginLinkButton);
176 Controls.Add (loginImageButton);
180 protected virtual void OnLoggedOut (EventArgs e)
182 // this gets called only if the authentication was successful
183 EventHandler loggedOut = (EventHandler) Events [loggedOutEvent];
184 if (loggedOut != null)
188 protected virtual void OnLoggingOut (LoginCancelEventArgs e)
190 // this gets called before OnAuthenticate so we can abort the authentication process
191 LoginCancelEventHandler loggingOut = (LoginCancelEventHandler) Events [loggingOutEvent];
192 if (loggingOut != null)
193 loggingOut (this, e);
196 protected internal override void OnPreRender (EventArgs e)
198 base.OnPreRender (e);
199 // documentation says we select Login*|Logout* here
200 // but tests shows that the selection is done even
201 // if OnPreRender is never called
204 protected internal override void Render (HtmlTextWriter writer)
209 RenderContents (writer);
212 protected internal override void RenderContents (HtmlTextWriter writer)
217 EnsureChildControls ();
219 bool authenticated = false;
221 Page.VerifyRenderingInServerForm (this);
222 authenticated = Page.Request.IsAuthenticated;
225 bool logoutImage = (LogoutImageUrl.Length > 0);
226 logoutLinkButton.Visible = authenticated && !logoutImage;
227 logoutImageButton.Visible = authenticated && logoutImage;
229 bool loginImage = (LoginImageUrl.Length > 0);
230 loginLinkButton.Visible = !authenticated && !loginImage;
231 loginImageButton.Visible = !authenticated && loginImage;
233 if (logoutLinkButton.Visible) {
234 logoutLinkButton.Text = LogoutText;
235 logoutLinkButton.Render (writer);
236 } else if (logoutImageButton.Visible) {
237 logoutImageButton.AlternateText = LogoutText;
238 logoutImageButton.ImageUrl = LogoutImageUrl;
239 writer.AddAttribute(HtmlTextWriterAttribute.Name, logoutImageButton.UniqueID);
240 logoutImageButton.Render (writer);
241 } else if (loginLinkButton.Visible) {
242 loginLinkButton.Text = LoginText;
243 loginLinkButton.Render (writer);
244 } else if (loginImageButton.Visible) {
245 loginImageButton.AlternateText = LoginText;
246 loginImageButton.ImageUrl = LoginImageUrl;
247 writer.AddAttribute(HtmlTextWriterAttribute.Name, loginImageButton.UniqueID);
248 loginImageButton.Render (writer);
252 [MonoTODO ("for design-time usage - no more details available")]
253 protected override void SetDesignModeState (IDictionary data)
255 base.SetDesignModeState (data);
260 public event EventHandler LoggedOut {
261 add { Events.AddHandler (loggedOutEvent, value); }
262 remove { Events.RemoveHandler (loggedOutEvent, value); }
265 public event LoginCancelEventHandler LoggingOut {
266 add { Events.AddHandler (loggingOutEvent, value); }
267 remove { Events.RemoveHandler (loggingOutEvent, value); }
272 private void LogoutClick (object sender, CommandEventArgs e)
274 LoginCancelEventArgs lcea = new LoginCancelEventArgs (false);
279 FormsAuthentication.SignOut ();
281 switch (LogoutAction) {
282 case LogoutAction.Refresh:
283 HttpContext.Current.Response.Redirect (Page.Request.Url.AbsoluteUri);
285 case LogoutAction.RedirectToLoginPage:
286 FormsAuthentication.RedirectToLoginPage ();
288 case LogoutAction.Redirect:
289 string url = LogoutPageUrl;
291 url = Page.Request.Url.AbsoluteUri;
292 HttpContext.Current.Response.Redirect (url);
299 private void LoginClick (object sender, CommandEventArgs e)
301 FormsAuthentication.RedirectToLoginPage ();