简体   繁体   English

尝试使用ASP.NET成员资格在MVC3中获得用户的userID发布身份验证

[英]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: 当用户登录到我们的MVC3 Web应用程序时,我们需要处理一些特定的事情,因此将其连接到global.asax.cs中,如下所示:

    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(); args.Context.Request.LogonUserIdentity != null行中,该行args.Context.Request.LogonUserIdentity != null其中,以避免绊倒Membership.GetUser(); args.Context.Request.LogonUserIdentity != null 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. 我怀疑使用FormsAuthentication_OnAuthenticate的选择不合适,我在寻找合适的事件和功能( 生命周期和事件 ),但没有明确的答案。

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. 使用global.asax事件Application_AuthorizeRequest而不是FormsAuthentication_OnAuthenticate为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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