在MVC项目上模拟CryptographicException?

时间:2017-09-15 08:23:34

标签: c# asp.net-mvc exception

我有一个项目(SharePoint-Addin),会在我的客户计算机上不时抛出CryptographicException。甚至在进入任何Controller-Action之前就会发生此异常。

我想抓住这个错误,分析它并做出相应的反应。 (目前我的猜测是这是某种令牌到期,所以我想简单地显示登录页面,如果发生这种情况)

我收到了以下代码:

  protected void Application_Error(object sender, EventArgs e)
        {
            _log.Error("ExceptionError occured ");
            Exception exception = Server.GetLastError();
            if (exception!=null)
            {
                _log.Error(exception);
                if (exception is CryptographicException)
                {   
                   Response.Redirect(loginPage, true);
                }
            }
       }

这将(希望)有效,但这个问题不是关于代码,而是关于如何强制加密异常。

我可以在请求中添加“邪恶标题”,这样会触发吗?

1 个答案:

答案 0 :(得分:0)

请看MSDN:

https://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptographicexception(v=vs.110).aspx

private void StringConstructor()
{
    // Construct a CryptographicException using a custom error message.
    string errorMessage = ("Unexpected Operation exception.");
    CryptographicException cryptographicException =
        new CryptographicException(errorMessage);
    Console.WriteLine("Created a CryptographicException with the " + 
        "following error message: " + errorMessage);
}

或只是

throw new CryptographicException(errorMessage);
相关问题