What is the best place to detect user login when using azure acs and mvc3?

I want to be able to detect when a user subscribes to my application using passive variables, so I can add them to my database if this is the first time I use my application. Right now I am subscribing to WSFederationAuthenticationModule.SignedIn, but I feel like I'm missing something. Basically, I’m not sure that it’s best to sign up for an event, I got it to work inside PostAuthenticateRequest, but it hacked a bit. Any suggestions?

this code is from global.asax

    public override void Init()
    {

        base.Init();

        PostAuthenticateRequest += (s, e) =>
        {
            try
            {
                FederatedAuthentication.WSFederationAuthenticationModule.SignedIn -= SignedIn;
            }
            finally
            {
                FederatedAuthentication.WSFederationAuthenticationModule.SignedIn += SignedIn;
            }

        };


    }


    private void SignedIn(object sender, EventArgs e)
    {
       //do something
    }

EDIT:

, , SignedIn. - , :) . .

    private static bool isFirstRequest = true;

    public override void Init()
    {


        base.Init();

        PostAuthenticateRequest += (s, e) => { 
        if (isFirstRequest)
        {
             FederatedAuthentication
                 .WSFederationAuthenticationModule.SignedIn += SignedIn;
             isFirstRequest = false;
        }

        };

    }


    private void SignedIn(object sender, EventArgs e)
    {

        //do something   

    }

EDIT: . , azure, , , . , , , .

+3
2

SignedIn , PostAuthenticateRequest? , ( Global.asax), , :

public class MvcApplication : System.Web.HttpApplication
{
    ...

    protected void Application_Start()
    {
        ...

        FederatedAuthentication.ServiceConfigurationCreated += (s, e) =>
        {
            FederatedAuthentication.WSFederationAuthenticationModule.SignedIn += new EventHandler(OnUserSignedIn);
        };
    }

    private void OnUserSignedIn(object sender, EventArgs e)
    {
        // Custom logic here.   
    }
}

SignedIn - , . . , SignedIn , :

Federated Authentication Module diagram

: http://msdn.microsoft.com/en-us/library/ee517293.aspx

+6

, ClaimsAuthenticationManager. , ,

public virtual IClaimsPrincipal Authenticate(string resourceName, IClaimsPrincipal incomingPrincipal);

, , , , (.. ). , .

:

public override IClaimsPrincipal Authenticate(string resourceName, IClaimsPrincipal incomingPrincipal)
    {
        if (incomingPrincipal.Identity.IsAuthenticated)
        {
            var identity = incomingPrincipal.Identity as IClaimsIdentity;                
            User user = null;

            // Get name identifier and identity provider
            var nameIdentifierClaim = identity.Claims.SingleOrDefault(c => c.ClaimType.Equals(ClaimTypes.NameIdentifier, StringComparison.OrdinalIgnoreCase));
            var identityProviderClaim = identity.Claims.SingleOrDefault(c => c.ClaimType.Equals(CustomClaimTypes.IdentityProviderClaimType, StringComparison.OrdinalIgnoreCase));

            if (nameIdentifierClaim == null || identityProviderClaim == null)
            {
                throw new AuthenticationErrorException("Invalid claims", "The claims provided by your Identity Provider are invalid. Please contact your administrator.");
            }

            try
            {
                //checking the database here...
                using (var context = new CloudContext())
                {
                    user = (from u in context.Users
                            where u.IdentityProvider == identityProviderClaim.Value &&
                                  u.NameIdentifier == nameIdentifierClaim.Value &&
                                  !u.Account.PendingDelete
                            select u).FirstOrDefault();
                }
            }
            catch (System.Data.DataException ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine(ex.InnerException);
                throw;
            }

        }

        return incomingPrincipal;
    }

web.config <microsoft.identitymodel> :

      <claimsAuthenticationManager type="CloudAnalyzer.UI.Security.CloudAnalyzerClaimsAuthenticationManager" />

, : Windows Azure Marketplace. Window Azure Marketplace, , ACS.

+2

All Articles