Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Don';异常错误期间不显示数据库连接信息_Asp.net - Fatal编程技术网

Asp.net Don';异常错误期间不显示数据库连接信息

Asp.net Don';异常错误期间不显示数据库连接信息,asp.net,Asp.net,我们正在使用C#和ASP.NET。在那里,我们了解到数据库连接信息在DEV&PROD服务器上的异常错误消息中可见。这会带来安全风险。因此,我正在编写一个脚本,它将从错误消息中删除连接字符串信息,这样每个人都不会看到它 我很确定web.config中有一个设置也不允许显示数据库连接字符串。如果你知道怎么做,那么请随意给我们指路 否则,下面是我编写的脚本,但它不会工作,因为错误消息不是数据库连接字符串,而是数据库对象&密码中可能还有“;”字符。因此,我欢迎更好的建议 public void

我们正在使用C#和ASP.NET。在那里,我们了解到数据库连接信息在DEV&PROD服务器上的异常错误消息中可见。这会带来安全风险。因此,我正在编写一个脚本,它将从错误消息中删除连接字符串信息,这样每个人都不会看到它

我很确定web.config中有一个设置也不允许显示数据库连接字符串。如果你知道怎么做,那么请随意给我们指路

否则,下面是我编写的脚本,但它不会工作,因为错误消息不是数据库连接字符串,而是数据库对象&密码中可能还有“;”字符。因此,我欢迎更好的建议

    public void EmailErrorMsgTest()
    {
        //var testcase = "A problem has occurred on this web site. Please try again.  If this error continues, please contact support.";
        var testcase = "Resolution of the dependency failed, type = 'DealerSoft.Service.Interfaces.IIdentityService', name = '(none)'.Exception occurred while: Calling constructor DealerSoft.DataAccess.DbContext(DealerSoft.DataAccess.IConnectionFactory connectionFactory, DealerSoft.Framework.Resolver.IUserResolverService userResolverService).Exception is: Exception - Data Source = foo.DealerSoftwebfoo.com,1200; Initial Catalog = DatabaseFoo; User ID = UserIdFoo; Password = PasswordFoo; MultipleActiveResultSets = true---------------------------------------------- - At the time of the exception, the container was: Resolving DealerSoft.Service.Interfaces.IIdentityService,(none)Resolving parameter 'repository' of constructor DealerSoft.Service.Services.IdentityService(DealerSoft.DataAccess.Repository.Repository repository, DealerSoft.DataAccess.IUnitOfWork unitOfWork) Resolving DealerSoft.DataAccess.Repository.Repository,(none)Resolving parameter 'context' of constructor DealerSoft.DataAccess.Repository.Repository(DealerSoft.DataAccess.DbContext context) Resolving DealerSoft.DataAccess.DbContext,(none)Calling constructor DealerSoft.DataAccess.DbContext(DealerSoft.DataAccess.IConnectionFactory connectionFactory, DealerSoft.Framework.Resolver.IUserResolverService userResolverService)";

        testcase = SecurityRiskErrorMessageCleanup(testcase);
    }

    private string SecurityRiskErrorMessageCleanup(string errorMessage)
    {
        int startPos = -1;
        int endPos = -1;
        var msg = errorMessage.ToLower();

        // Remove database connection string.
        startPos = msg.IndexOf(@"data source =");
        if (startPos >= 0)
        {
            var tmpPos = msg.IndexOf(@"password =", startPos + 1);
            endPos = msg.IndexOf(";", tmpPos + 1);
            if (endPos > startPos && startPos >= 0)
            {
                var tmpStr = errorMessage.Substring(startPos, (endPos - startPos) + 1);
                errorMessage = errorMessage.Replace(tmpStr, "");
            }
        }
        startPos = -1;
        endPos = -1;

        // Future cleanup.
        // N/A.

        return errorMessage;
    }

在web.config文件中,您可以设置在
RemoteOnly
用户上显示自定义错误页面,而本地用户仍将看到详细错误页面

<system.web>
    <customErrors defaultRedirect="YourErrorPage.aspx"
                  mode="RemoteOnly">
      <error statusCode="500"
             redirect="SpecificErrorPage.aspx"/>
    </customErrors>
  </system.web>


我们对生成/捕获错误的代码了解不够,但在我看来,您应该首先注意不要将连接字符串信息附加到错误中(当然,除非您使用的是第三方组件)。您必须在此答案中添加多一点文本,使其连接到所问的问题…虽然您确实可以通过这种方式隐藏错误的详细信息,但这并不能解决将敏感信息泄漏到错误消息中的真正问题。确定,但不会向用户显示错误消息。那么这个物体会有机会被使用吗?