Skip to main content

Posts

Showing posts from 2015

log4net file appender file location

In log4net, we can use the FileAppender to log information into text files. There are two ways to specify the file: 1. Specify the file name in absolutely path. For example: <log4net> <appender name = "FileAppender" type = "log4net.Appender.FileAppender" > <file value = "c:\logs\log-file.txt" /> <appendToFile value = "true" /> <lockingModel type = "log4net.Appender.FileAppender+MinimalLock" /> <layout type = "log4net.Layout.PatternLayout" > <conversionPattern value = "%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> </log4net> Obviously, the file will be created in c:\logs\log-file.txt 2. Specify the file name in relative path. For example: <log4net> <appender name = "FileAppender" type = "log4net.Appender.FileAppender" &g

ComponentSpace SAML message expired issue

When I used ComponentSpace SAML2 API to process the SAML message, I got the messsage expired error. The code I used to check if the message is expired or not is: if (!samlAssertion.Conditions.IsWithinTimePeriod()) {     // Message expired } It seems the IsWithinTimePeriod() function uses the local time to compare with NotBefore and NotOnOrAfter attributes in the SAML message. Since SAML message uses UTC time for all the datetime related attributes, the  IsWithinTimePeriod() function often returns false because of the big difference between local time and UTC time. In order to check if the message is expired or not, we cannot use the IsWithinTimePeriod() function. I used following code to check the exipiration: if (!(DateTime.Now.ToUniversalTime() > samlAssertion.Conditions.NotBefore && DateTime.Now.ToUniversalTime() < samlAssertion.Conditions.NotOnOrAfter)) {      // Message expired }

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message: System . Security . Cryptography . CryptographicException : The system cannot find the file specified . at System . Security . Cryptography . CryptographicException . ThrowCryptogaphicException ( Int32 hr ) at System . Security . Cryptography . X509Certificates . X509Utils . _LoadCertFromBlob ( Byte [] rawData , IntPtr password , UInt32 dwFlags , Boolean persistKeySet , SafeCertContextHandle & pCertCtx ) at System . Security . Cryptography . X509Certificates . X509Certificate . LoadCertificateFromBlob ( Byte [] rawData , Object password , X509KeyStorageFlags keyStorageFlags ) at System . Security . Cryptography . X509Certificates . X509Certificate2 .. ctor ( Byte [] rawData , String password , X509KeyStorageFlags keyStorageFlags ) In orde

Post dictionary parameters or IEnumerable parameters to MVC controller

I found a very good artical about how to pass the dictionary or IEnumerable<T> parameters to MVC controller . http://www.hanselman.com/blog/ ASPNETWireFormatForModelBindingToArraysListsCollectionsDicti onaries.aspx So if your action requires array of objects, say Person: public ActionResult ProcessAction(Person[] people) Then your MVC view HTML should look like: < input type = "text" name = "people[0].FirstName" value = "George" /> < input type = "text" name = "people[0].LastName" value = "Washington" /> < input type = "text" name = "people[1].FirstName" value = "Abraham" /> < input type = "text" name = "people[1].LastName" value = "Lincoln" /> < input type = "text" name = "people[3].FirstName" value = "Thomas" /> < input type = "text" name = &quo

Using SQLite in .NET with Visual Studio 2013

SQLite is a software library that implements a self-contained, serverless, zero- configuration, transactional SQL database engine. SQLite is a popular choice as embedded database for local/client storage. In order to use SQLite in your .NET application, you can run following command in Visual Studio 2013 Package Manager Console. PM> Install-Package System.Data.SQLite  Once the package has been successfully installed, your project will have proper reference to SQLite dll. In the class you would like to use SQLite, add following "using" statement. using System.Data.SQLite; Here is the sample code that uses SQLite: using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.Linq; using System.Text; namespace DataAccessLayer {     public class SqlLiteHelper     {         public static int ExecuteNonQuery( string strConnectionString, string strSqlCommand, SQLiteParameter [] sqlParams)         {           

Custom error page in MVC 4

MVC 4 project has already has an Error.cshtml in Views\Shared folder. In order to use the Error.cshtml, you have to do following steps: 1. Set the customError mode to On inside web.config file under <system.web> element <customErrors mode = " On " /> 2. In the FilterConfig.cs file inside App_Start folder, make sure the filters.Add(new HandleErrorAttribute()) is there.     public class FilterConfig     {         public static void RegisterGlobalFilters( GlobalFilterCollection filters)         {             filters.Add( new HandleErrorAttribute ());         }     } 3. Edit Error.cshtml inside \Views\Shared folder to show error message @mod el System.Web.Mvc. HandleErrorInfo @{     ViewBag.Title = "Error" ;     Layout = "~/Views/Shared/_Layout.cshtml" ; } <div>An error has occurred</div> @ if (Model != null && HttpContext .Current.IsDebuggingEnabled) {     < div >        < p >             < b >Excep