【问题标题】:Trying to get a user's userID post authentication in MVC3 using ASP.NET memberships尝试使用 ASP.NET 成员资格在 MVC3 中获取用户的用户 ID 后身份验证
【发布时间】:2012-02-08 07:17:24
【问题描述】:

当用户登录到我们的 MVC3 网络应用程序时,我们需要处理一些特定的事情,所以它在 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 不是一个合适的选择,我正在寻找合适的事件和功能(lifetime and events)没有明确的答案。

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

【问题讨论】:

  • Hm... LogonUserIdentity 当前 Microsoft Internet 信息服务 (IIS) 身份验证设置的 WindowsIdentity 对象。您正尝试从 FormsAuthentication_OnAuthenticate 访问此属性。看起来很奇怪。
  • 随便试试Application_AcquireRequestState

标签: asp.net-mvc forms-authentication


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2016-02-02
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多