You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kentico 12 SP自定义认证事件处理器插入自定义表项报错求助

Kentico 12 SP Windows认证登录日志插入失败排查

我们基于Kentico 12 SP结合Windows认证搭建系统,需要记录特定角色用户的登录行为。针对SecurityEvents.Authenticate.Execute事件编写了自定义处理器,代码如下:

if (e.User.IsInRole("bsi-sfr-lg-hcwp_patientroster", "Internal") || e.User.SiteIndependentPrivilegeLevel.Equals(UserPrivilegeLevelEnum.GlobalAdmin))
{
    string EIN = e.User.UserName.Substring(4, 6);
    U.SamAccountName = EIN;
    PrincipalSearcher searcher = new PrincipalSearcher(U);
    UserPrincipal principal = (UserPrincipal)searcher.FindOne();
    if (principal != null)
    {
        Login = principal.DisplayName;
    }
    else
    {
        Login = "Error getting Name";
    }
    DataClassInfo classInfo = DataClassInfoProvider.GetDataClassInfo(AuthenticationLogTable);
    if (classInfo != null)
    {
        CustomTableItem NewAuthLog = new CustomTableItem(AuthenticationLogTable);
        NewAuthLog.SetValue("Login", e.User.UserName);
        NewAuthLog.SetValue("DisplayName", Login);
        if (e.User.SiteIndependentPrivilegeLevel.Equals(UserPrivilegeLevelEnum.GlobalAdmin))
            NewAuthLog.SetValue("UserRole", "Global Admin");
        else if (e.User.IsInRole("bsi-sfr-lg-hcwp_patientroster", "Internal"))
            NewAuthLog.SetValue("UserRole", "bsi-sfr-lg-hcwp_patientroster");
        // Add additional roles here as an else if
        
        NewAuthLog.Insert();
    }
}

执行到NewAuthLog.Insert()时触发错误,错误调用栈:

Description:at CMS.CustomTables.CustomTableItem.InsertData()
at CMS.DataEngine.AbstractInfoProvider`3.SetInfo(TInfo info)
at CustomAuthenticationHandler.OnAuthentication(Object sender, AuthenticationEventArgs e) in C:\Sites\internal-test.vitalant.org\server\CMS\App_Code\_CustomHandlers\CustomAuthenticationHandler.cs:line 68

已通过LogEvent验证插入前NewAuthLog的所有字段值均符合预期,事件日志能正常写入。


排查与解决方向:

  • 权限校验:检查事件处理器执行上下文的账号是否拥有目标自定义表AuthenticationLogTable的插入权限。在Kentico后台“自定义表”模块中,确认对应账号(或应用池身份)具备该表的Create权限。
  • 字段约束检查:核对AuthenticationLogTable的字段定义,确认所有必填字段已赋值、字段数据类型与传入值匹配、字符串长度未超出限制。即使日志显示值符合预期,也可能存在隐藏的必填字段未设置。
  • 上下文切换:SecurityEvents.Authenticate触发时用户上下文可能未完全初始化,尝试切换到管理员上下文执行插入:
    using (new CMSActionContext { User = UserInfoProvider.GetUserInfo("admin") })
    {
        NewAuthLog.Insert();
    }
    
  • 事务冲突处理:当前事件可能处于未完成的事务中,导致插入无法提交。可将插入操作放入独立事务,或检查Kentico事务机制对当前操作的影响。
  • 表名正确性验证:确认AuthenticationLogTable常量对应的是自定义表的代码名称(而非后台显示名称),通过Kentico后台“自定义表”模块核对代码名称是否一致。

内容的提问来源于stack exchange,提问作者Brian Gosnell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:45:41