Asp.net mvc 2 MVC2属性-什么';这是怎么回事? 背景

Asp.net mvc 2 MVC2属性-什么';这是怎么回事? 背景,asp.net-mvc-2,authorize,asp.net-authorization,authorize-attribute,Asp.net Mvc 2,Authorize,Asp.net Authorization,Authorize Attribute,我有一个MVC2项目,在项目级web.config文件中启用了表单身份验证: <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" defaultUrl="~/Magic" name="MagicAuthCookie" slidingExpiration="false"

我有一个MVC2项目,在项目级web.config文件中启用了表单身份验证:

<authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn"
           defaultUrl="~/Magic"
           name="MagicAuthCookie"
           slidingExpiration="false"                    
           timeout="10" />
    </authentication>
感受流动 调试项目时,我最初会被重定向到web.config中指定的~/Account/LogOn controller/action:

    public ActionResult LogOn(string returnUrl)
    {
        if (ModelState.IsValid && MembershipService.ValidateUser(User.Identity.Name, "")){
            *... do some stuff here ...*
        }
    }
注意:此时控制器的“用户”属性为空

检查ModelState后,它将转到我的会员服务,即AccountModels.ValidateUser(用户名、密码):

转到MagicMembershipProvider.ValidateUser(用户名、密码):

最后转到BaseMembershipProvider.ValidateUser(用户名、密码):

服务器变量由一个自定义isapi.dll在早期设置,该自定义isapi.dll拦截所有调用并进行身份验证。如果存在值,则认为用户是可信的。此方法返回一个简单的布尔值。在我的代码中没有其他地方引用或传递此变量

这个布尔值被传递回执行树(因为没有更好的词)到原始的
LogOn(string returnUrl)
方法,该方法启动了所有的操作

唱反调者 现在,在这一点上,不知何故,User属性不再是null,User.Identity.Name属性从服务器变量设置为MagicUserName。我没有这样做

发生了什么事? 这种行为不是我想要的,但我不知道在哪里/如何停止它,因为我从未告诉框架这样做。我假设它在默认AuthorizeAttribute实现中的某个地方,但是它如何知道使用我检查过的这个随机服务器变量呢


如有任何启发,我们将不胜感激。

这是默认情况下表单身份验证的工作方式,与ASP.NET MVC无关。创建身份验证cookie()以指示用户成功签名时,表单身份验证模块将使用此cookie(IPrincipal设置在
FormsAuthenticationModule.OnAuthenticate
方法中)在后续请求中填充用户属性。您可以编写自定义的
Authorize
属性,并将用户属性设置为您喜欢的任何属性。此技术通常用于创建自定义主体。

它不太可能是
authorized属性。你说你被重定向到你的登录页面,这意味着它正在正确地执行它的工作,在这一点上,它已经完成,不再参与。由于ASP.NET MVC是开源的,因此您可以查看实际文件,您将看到
User.Identity.Name
已被访问但未分配到该文件。如果您从isapi发布相关代码,可能会有所帮助。另外,可能不相关,但您的基本提供程序ValidateUser看起来像是应用时出现的编译错误!添加到字符串变量。
[Authorize]
public ActionResult Index()
{
    MagicViewModel avm = new MagicViewModel { MagicDate = DateTime.Now.ToString("MM-dd-yyyy") };
    return View(avm);
}
    public ActionResult LogOn(string returnUrl)
    {
        if (ModelState.IsValid && MembershipService.ValidateUser(User.Identity.Name, "")){
            *... do some stuff here ...*
        }
    }
    public bool ValidateUser(string userName, string password)
    {
        return _provider.ValidateUser(userName, password);
    }
public override bool ValidateUser(string username, string password)
{
    return base.ValidateUser(username, password);
}
public override bool ValidateUser(string username, string password)
{
    string magicUserName = 
        HttpContext.Current.Request.ServerVariables["MAGIC_USER_NAME"];
    return (!magicUserName == "");
}