简体   繁体   中英

Trying to get a user's userID post authentication in MVC3 using ASP.NET memberships

We need to process something specific when the user logs into our MVC3 web app, so it's wired inside global.asax.cs as follows:

    public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
    {
        if (args.Context.Request.LogonUserIdentity != null)
        {
            MembershipUser mu = Membership.GetUser();
            // blah blah ... kick off something special
            // for logged in users here
        }
    }

Right now I'm getting the exception

System.InvalidOperationException: This method can only be called after the authentication event.

at the line args.Context.Request.LogonUserIdentity != null which is in there to avoid tripping over Membership.GetUser(); I suspect the choice of using FormsAuthentication_OnAuthenticate is not an appropriate choice and I'm hunting for the appropriate event and function ( lifetime and events ) without a clear answer in sight.

Am I really off in the wrong direction on this one? What is the best event/function to hook into to process this case? Thanks!

I was running into the same issue. Using the global.asax event Application_AuthorizeRequest instead of FormsAuthentication_OnAuthenticate worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM