Directory Programming .NET

Active Directory and ADAM programming support for .NET developers
Welcome to Directory Programming .NET Sign in | Join | Help
in Search

Is it possible to subscribe to adfs agent events?

Last post 05-16-2009, 12:37 PM by joe. 3 replies.
Sort Posts: Previous Next
  •  05-12-2009, 2:50 PM 6436

    Is it possible to subscribe to adfs agent events?

    Hi,

    I like to run custom code each time a user is authenticated by ADFS. This code should run on the agent. My custom code will write a cookie to the client to track if the user has been logged on. I have to write a custom cookie because I have to know if the user has been logged on even when the user is on a page which is not controlled by ADFS. On those pages I want to show the link 'sign in' when the user is not logged on and a link 'sign out' when the user has been logged on. It now even works this way within our solution. But now I write the cookie in a method which is normally called after the user has been logged on (GetUserFromAdam) but it would be better if I could subscribe to a ADFS event. I see there is an event Authenticate in WebSsoAuthenticationModule but I don't know how to handle this event (global.asax?). Also, it would also be better to handle a Autenticated event (so I know the authentication was successful).

    Thanks,

    René 

  •  05-13-2009, 2:27 PM 6437 in reply to 6436

    Re: Is it possible to subscribe to adfs agent events?

    The WebSsoAuthenticationModule definitely fires an authenticate event that you can handle. I'm not entirely sure what the syntax is for handling an event in a loaded module, but you should at least be able to write some code that would loop through the list of modules in the context, find this module and add a handler to its event.

    I think global.asax has some "syntactic sugar" to make this easier to do but I don't know what that is so I only recommended the "hard" way to do it as I'm more certain that it would work.

    The event is called "Authenticate".

  •  05-15-2009, 6:08 PM 6458 in reply to 6437

    Re: Is it possible to subscribe to adfs agent events?

    Ok, it works.

    Below the event in the global.asax. It is only fired when authentication succeeded, the authenticated user is available in the handler argument:

    public void WebSsoAuthenticationModule_Authenticate(object sender, WebSsoAuthenticationEventArgs e)
    {
      using (StreamWriter w = new StreamWriter(@"e:\authentication3.log"))
      {
          w.WriteLine(e.Identity.Name);
      }
    }

    It took me hours to find out how the event handler has to look. Using reflector I found the answer. Global.asax event handlers are coupled to events of http modules by using the key of the module. The key is configured in the web.config:

    For ADFS the default configuration looks like this.

    <httpModules>
                <add
                    name="Identity Federation Services Application Authentication Module"
                    type="System.Web.Security.SingleSignOn.WebSsoAuthenticationModule, System.Web.Security.SingleSignOn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null" />
            </httpModules>

    The key is  then "Identity Federation Services Application Authentication Module". In the global.asax a method should be created with this name. Of course this is not possible because it's not allowed to create method names with spaces. Therefore you have to change the name. If you change it to WebSsoAuthenticationModule. The global.asax method is:

    public void WebSsoAuthenticationModule_Authenticate(object sender, WebSsoAuthenticationEventArgs e)
    {
    }

    Regards,

    René

  •  05-16-2009, 12:37 PM 6461 in reply to 6458

    Re: Is it possible to subscribe to adfs agent events?

    Glad you figured it out.  Thanks for posting the details.

View as RSS news feed in XML