MVC3 Web应用程序内的自定义验证模块

时间:2012-04-30 20:34:21

标签: asp.net-mvc authentication httpmodule

上下文:

我们有一个内部Asp.Net Web应用程序,配置为使用Windows身份验证。作为此身份验证方面的一部分,我们有一个HttpModule,它基本上抓取HttpContext.Current.Identity.Name并返回一个UserInfo对象,该对象将被放入HttpContext.Items集合中。

在通过MVC3迁移它时,我有一个基本控制器和OnActionExecuting,我根本无法在集合中看到这个UserInfo项。任何见解都会很棒。这是我的设置:

BaseController:

protected override void OnActionExecuting(ActionExecutingContext ctx)
        {
            if (ctx.HttpContext.Items["UserInfo"] != null)
            {
                UserInfo currentUser = (UserInfo)ctx.HttpContext.Items["UserInfo"];
                dynamic viewBag = ctx.Controller.ViewBag;
                viewBag.CurrentUser = currentUser;
            }
            else
            {
                // Unauthorized do something
            }            

            base.OnActionExecuting(ctx);
        }

的web.config:

<system.web>
    <httpModules>
      <add type="WFS.SIG.Client.Security.Authentication.WindowsAuthentication, WFS.SIG.Client.Security" name="AuthenticationModule"/>
    </httpModules>
</system.web>....

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="AuthenticationModule" type="WFS.SIG.Client.Security.Authentication.WindowsAuthentication, WFS.SIG.Client.Security" />
    </modules>
</system.webServer>

1 个答案:

答案 0 :(得分:0)

我认为您的代码应如下所示:

protected override void OnActionExecuting(ActionExecutingContext ctx) 
{ 
    if (ctx.HttpContext.Items["UserInfo"] != null) 
    { 
        UserInfo currentUser = (UserInfo)ctx.HttpContext.Items["UserInfo"]; 
        ViewBag.CurrentUser = currentUser; 
    } 
    else 
    { 
        // Unauthorized do something 
    }             
    base.OnActionExecuting(ctx); 
} 

对HttpContext的访问应该像这样工作。但您可以直接访问ViewBag。

您可以检查您的authenticaton模块是否真的被调用并且是否在HttpContext中存储了一个对象?你能设置一个断点吗?