/// <summary>
 /// Handles the GetRoles event of the roleManager control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.Web.Security.RoleManagerEventArgs"/> instance containing the event data.</param>
 private void roleManager_GetRoles(object sender, RoleManagerEventArgs e)
 {
     if (this.application.User is OAuthPrincipal)
     {
         e.RolesPopulated = true;
     }
 }
Esempio n. 2
0
		void OnPostAuthenticateRequest (object sender, EventArgs args)
		{
			HttpApplication app = (HttpApplication)sender;

			/* if we're disabled, bail out early */
			if (_config == null || !_config.Enabled)
				return;

			/* allow the user to populate the Role */
			RoleManagerEventHandler eh = events [getRolesEvent] as RoleManagerEventHandler;
			if (eh != null) {
				RoleManagerEventArgs role_args = new RoleManagerEventArgs (app.Context);

				eh (this, role_args);

				if (role_args.RolesPopulated)
					return;
			}

			RolePrincipal principal;

			HttpCookie cookie = app.Request.Cookies [_config.CookieName];

			IIdentity currentIdentity = app.Context.User.Identity;
			if (app.Request.IsAuthenticated) {
				if (cookie != null) {
					if (!_config.CacheRolesInCookie)
						cookie = null;
					else if (_config.CookieRequireSSL && !app.Request.IsSecureConnection) {
						cookie = null;
						ClearCookie (app, _config.CookieName);
					}
						
				}

				if (cookie == null || String.IsNullOrEmpty (cookie.Value))
					principal = new RolePrincipal (currentIdentity);
				else
					principal = new RolePrincipal (currentIdentity, cookie.Value);
			}
			else {
				/* anonymous request */

				if (cookie != null) {
					ClearCookie (app, _config.CookieName);
				}

				principal = new RolePrincipal (currentIdentity);
			}

			app.Context.User = principal;
			Thread.CurrentPrincipal = principal;
		}
		/// <summary>
		/// Handles the GetRoles event of the roleManager control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Web.Security.RoleManagerEventArgs"/> instance containing the event data.</param>
		private void roleManager_GetRoles(object sender, RoleManagerEventArgs e) {
			if (this.application.User is DotNetOpenAuth.OAuth.ChannelElements.OAuthPrincipal) {
				e.RolesPopulated = true;
			}
		}
Esempio n. 4
0
 private void OnEnter(object source, EventArgs eventArgs)
 {
     if (!Roles.Enabled)
     {
         if (HttpRuntime.UseIntegratedPipeline)
         {
             ((HttpApplication)source).Context.DisableNotifications(RequestNotification.EndRequest, 0);
         }
     }
     else
     {
         HttpApplication application = (HttpApplication)source;
         HttpContext     context     = application.Context;
         if (this._eventHandler != null)
         {
             RoleManagerEventArgs e = new RoleManagerEventArgs(context);
             this._eventHandler(this, e);
             if (e.RolesPopulated)
             {
                 return;
             }
         }
         if (Roles.CacheRolesInCookie)
         {
             if (context.User.Identity.IsAuthenticated && (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
             {
                 try
                 {
                     HttpCookie cookie = context.Request.Cookies[Roles.CookieName];
                     if (cookie != null)
                     {
                         string encryptedTicket = cookie.Value;
                         if ((encryptedTicket != null) && (encryptedTicket.Length > 0x1000))
                         {
                             Roles.DeleteCookie();
                         }
                         else
                         {
                             if (!string.IsNullOrEmpty(Roles.CookiePath) && (Roles.CookiePath != "/"))
                             {
                                 cookie.Path = Roles.CookiePath;
                             }
                             cookie.Domain = Roles.Domain;
                             context.SetPrincipalNoDemand(this.CreateRolePrincipalWithAssert(context.User.Identity, encryptedTicket));
                         }
                     }
                 }
                 catch
                 {
                 }
             }
             else
             {
                 if (context.Request.Cookies[Roles.CookieName] != null)
                 {
                     Roles.DeleteCookie();
                 }
                 if (HttpRuntime.UseIntegratedPipeline)
                 {
                     context.DisableNotifications(RequestNotification.EndRequest, 0);
                 }
             }
         }
         if (!(context.User is RolePrincipal))
         {
             context.SetPrincipalNoDemand(this.CreateRolePrincipalWithAssert(context.User.Identity, null));
         }
         HttpApplication.SetCurrentPrincipalWithAssert(context.User);
     }
 }
        void OnPostAuthenticateRequest(object sender, EventArgs args)
        {
            HttpApplication app = (HttpApplication)sender;

            /* if we're disabled, bail out early */
            if (_config == null || !_config.Enabled)
            {
                return;
            }

            /* allow the user to populate the Role */
            RoleManagerEventHandler eh = events [getRolesEvent] as RoleManagerEventHandler;

            if (eh != null)
            {
                RoleManagerEventArgs role_args = new RoleManagerEventArgs(app.Context);

                eh(this, role_args);

                if (role_args.RolesPopulated)
                {
                    return;
                }
            }

            RolePrincipal principal;

            HttpCookie cookie = app.Request.Cookies [_config.CookieName];

            IIdentity currentIdentity = app.Context.User.Identity;

            if (app.Request.IsAuthenticated)
            {
                if (cookie != null)
                {
                    if (!_config.CacheRolesInCookie)
                    {
                        cookie = null;
                    }
                    else if (_config.CookieRequireSSL && !app.Request.IsSecureConnection)
                    {
                        cookie = null;
                        ClearCookie(app, _config.CookieName);
                    }
                }

                if (cookie == null || String.IsNullOrEmpty(cookie.Value))
                {
                    principal = new RolePrincipal(currentIdentity);
                }
                else
                {
                    principal = new RolePrincipal(currentIdentity, cookie.Value);
                }
            }
            else
            {
                /* anonymous request */

                if (cookie != null)
                {
                    ClearCookie(app, _config.CookieName);
                }

                principal = new RolePrincipal(currentIdentity);
            }

            app.Context.User        = principal;
            Thread.CurrentPrincipal = principal;
        }
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        private void OnEnter(Object source, EventArgs eventArgs)
        {
            if (!Roles.Enabled)
            {
                if (HttpRuntime.UseIntegratedPipeline)
                {
                    ((HttpApplication)source).Context.DisableNotifications(RequestNotification.EndRequest, 0);
                }
                return;
            }

            HttpApplication app     = (HttpApplication)source;
            HttpContext     context = app.Context;

            if (_eventHandler != null)
            {
                RoleManagerEventArgs e = new RoleManagerEventArgs(context);
                _eventHandler(this, e);
                if (e.RolesPopulated)
                {
                    return;
                }
            }

            Debug.Assert(null != context.User, "null != context.User");

            if (Roles.CacheRolesInCookie)
            {
                if (context.User.Identity.IsAuthenticated && (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
                {
                    // Try to create from cookie
                    try
                    {
                        HttpCookie cookie = context.Request.Cookies[Roles.CookieName];
                        if (cookie != null)
                        {
                            string cookieValue = cookie.Value;
                            // Ignore cookies that are too long
                            if (cookieValue != null && cookieValue.Length > MAX_COOKIE_LENGTH)
                            {
                                Roles.DeleteCookie();
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(Roles.CookiePath) && Roles.CookiePath != "/")
                                {
                                    cookie.Path = Roles.CookiePath;
                                }

                                cookie.Domain = Roles.Domain;
                                context.SetPrincipalNoDemand(CreateRolePrincipalWithAssert(context.User.Identity, cookieValue));
                            }
                        }
                    }
                    catch {  } // ---- exceptions
                }
                else
                {
                    if (context.Request.Cookies[Roles.CookieName] != null)
                    {
                        Roles.DeleteCookie();
                    }
                    // if we're not using cookie caching, we don't need the EndRequest
                    // event and can suppress it
                    if (HttpRuntime.UseIntegratedPipeline)
                    {
                        context.DisableNotifications(RequestNotification.EndRequest, 0);
                    }
                }
            }

            if (!(context.User is RolePrincipal))
            {
                context.SetPrincipalNoDemand(CreateRolePrincipalWithAssert(context.User.Identity));
            }

            HttpApplication.SetCurrentPrincipalWithAssert(context.User);
        }
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        private void OnEnter(Object source, EventArgs eventArgs) {
            if (!Roles.Enabled) {
                if (HttpRuntime.UseIntegratedPipeline) {
                    ((HttpApplication)source).Context.DisableNotifications(RequestNotification.EndRequest, 0);
                }
                return;
            }
            
            HttpApplication app = (HttpApplication)source;
            HttpContext context = app.Context;
            if (_eventHandler != null) {
                RoleManagerEventArgs e = new RoleManagerEventArgs(context);
                _eventHandler(this, e);
                if (e.RolesPopulated)
                    return;
            }
            
            Debug.Assert(null != context.User, "null != context.User");
            
            if (Roles.CacheRolesInCookie)
            {
                if (context.User.Identity.IsAuthenticated && (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
                {
                    // Try to create from cookie
                    try
                    {
                        HttpCookie cookie = context.Request.Cookies[Roles.CookieName];
                        if (cookie != null)
                        {
                            string cookieValue = cookie.Value;
                            // Ignore cookies that are too long
                            if (cookieValue != null && cookieValue.Length > MAX_COOKIE_LENGTH) {
                                Roles.DeleteCookie();
                            }
                            else {
                                if (!String.IsNullOrEmpty(Roles.CookiePath) && Roles.CookiePath != "/") {
                                    cookie.Path = Roles.CookiePath;
                                }

                                cookie.Domain = Roles.Domain;
                                context.SetPrincipalNoDemand(CreateRolePrincipalWithAssert(context.User.Identity, cookieValue));
                            }
                        }
                    }
                    catch {  } // ---- exceptions
                }
                else
                {
                    if (context.Request.Cookies[Roles.CookieName] != null)
                        Roles.DeleteCookie();
                    // if we're not using cookie caching, we don't need the EndRequest
                    // event and can suppress it
                    if (HttpRuntime.UseIntegratedPipeline) {
                        context.DisableNotifications(RequestNotification.EndRequest, 0);
                    }
                }
            }

            if (!(context.User is RolePrincipal))
                context.SetPrincipalNoDemand(CreateRolePrincipalWithAssert(context.User.Identity));

            HttpApplication.SetCurrentPrincipalWithAssert(context.User);
        }