尝试使用 ASP.NET 成员资格在 MVC3 中获取用户的用户 ID 后身份验证

发布于 2025-01-03 02:56:07 字数 923 浏览 3 评论 0原文

当用户登录到我们的 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
        }
    }

现在我

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

在 args.Context.Request.LogonUserIdentity 行收到异常! = null 是为了避免绊倒 Membership.GetUser(); 我怀疑使用 FormsAuthentication_OnAuthenticate 的选择不是一个合适的选择我正在寻找适当的事件和函数(一生和事件),但目前还没有明确的答案。

我在这方面真的走错方向了吗?处理这种情况的最佳事件/函数是什么?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-01-10 02:56:07

我遇到了同样的问题。使用 global.asax 事件 Application_AuthorizeRequest 而不是 FormsAuthentication_OnAuthenticate 对我有用。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文