public FileResult Download(string filename) {
filename = Server.HtmlEncode(filename);
string filePath = ConfigurationManager.AppSettings["SignatureFilesPath"];
string contentType = System.Net.Mime.MediaTypeNames.Application.Octet;
return File(Path.Combine(filePath, filename), contentType, filename);
}
3.11.2013
Download Any File Type in ASP.NET MVC 4 FileResult
Just simply specify a generic Octet stream as the contentType:
Configure MachineKey When Deploying App In A Web Farm
One thing to keep in mind when deploying in a web farm is that if you are using form authentication, or ViewState, and you want to keep your session alive across the web farm, you need to specify an machineKey in the web.config file. This is how it looks like:
Reference from msdn.
<system.web>
<machineKey validationKey="C82FF1CB5C0B723C0C6682274C87AE0A32E957D05846F505A1968D30EB41B7C34175741FFC45C20D89915C05D133EDE69154E9EDC6B82F7B76B24009B09DF299" decryptionKey="A941AD982309BBC34B1D17C2E5C3A27298CB32EBB29C7AD94F513ACCE09B1F06" validation="SHA1" decryption="AES"></machineKey>
</system.web>
You should generate your own machineKey, here's a machineKey generator.
Reference from msdn.
Setting Up Folder Permissions For Uploading Files in IIS 6
There are so many solutions to this problem. But it all boils down to the permissions given to the account to be used in accessing a particular resource. Assuming the website is saving a document to a Windows Share on another server. You will have to give Read/Write access to your account of choice (a system account maybe that password never expires) to that particular Share. Then in the web.config file, configure impersonation to use that same account like so ...
<system.web>
<identity impersonate="true" userName="dom1\sys-account" password="mypass"></identity />
Here's a post from msdn regarding IIS Authentication with impersonation. In particular about intranet scenarios.
3.08.2013
EF Code First Migrations - How To Sync To Production DB
It's very simple to push out the changes in the database across any db that would need the changes, be it your test, staging or production servers. All you have to do is to create a SQL script via Update-Database with a -Script flag:
Complete write up from Microsoft is here.
If you are doing this for the first time, and you need all the changes so far, you just need to do this:
There's a little hickup:
I got this error message, which means that the script generator is unable to remove duplicate variable declarations in the sql. The solution is to simply delete such duplicate declarations.
Msg 134, Level 15, State 1, Line 101 The variable name '@var0' has already been declared. Variable names must be unique within a query batch or stored procedure.
PM>Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstractBecause of the -Script switch, Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Neat.
Complete write up from Microsoft is here.
If you are doing this for the first time, and you need all the changes so far, you just need to do this:
PM>Update-Database -Script -SourceMigration: $InitialDatabaseHere's a sample PM console session:
PM> Update-Database -Script -SourceMigration: $InitialDatabase Applying code-based migrations: [201301240307501_Initial, 201302020143144_ModifiedEmailTableAndAddedSignatureTable, 201302020149599_AddedCreatorToSignatureRelation, 201302020158207_ChangeSignatureIdToNullable, 201302050122407_AddResultingImageFileToSignatureTable, 201302050154113_AddLabelToSignatureTable, 201302052134222_ChangedAddressToHave3LineItems, 201302090226121_ChangedSignatureFileToRequired, 201302121909589_AddedSalesItemClass, 201302192149411_AddedCustomerClass, 201302210141465_AddedLocationInIamProfileClass, 201302260305398_UpdatedCertificateClass]. Applying code-based migration: 201301240307501_Initial. Applying code-based migration: 201302020143144_ModifiedEmailTableAndAddedSignatureTable. Applying code-based migration: 201302020149599_AddedCreatorToSignatureRelation. Applying code-based migration: 201302020158207_ChangeSignatureIdToNullable. Applying code-based migration: 201302050122407_AddResultingImageFileToSignatureTable. Applying code-based migration: 201302050154113_AddLabelToSignatureTable. Applying code-based migration: 201302052134222_ChangedAddressToHave3LineItems. Applying code-based migration: 201302090226121_ChangedSignatureFileToRequired. Applying code-based migration: 201302121909589_AddedSalesItemClass. Applying code-based migration: 201302192149411_AddedCustomerClass. Applying code-based migration: 201302210141465_AddedLocationInIamProfileClass. Applying code-based migration: 201302260305398_UpdatedCertificateClass.Once the script is generated, Visual Studio opens the script for you to save. What I do is just simply copy and paste the resulting sql script to SQL Server Management Studio, then execute it there against the database. Done!
There's a little hickup:
I got this error message, which means that the script generator is unable to remove duplicate variable declarations in the sql. The solution is to simply delete such duplicate declarations.
Msg 134, Level 15, State 1, Line 101 The variable name '@var0' has already been declared. Variable names must be unique within a query batch or stored procedure.
How To Setup SecurityGuard.MVC4 And MvcInstaller.MVC4 For ASP.NET MVC 4 Application
I love this open source project called SecurityGuard. It is a complete ASP.NET Membership management system plugin for ASP.NET MVC 4 application.
To install via NuGet:
Follow these steps to setup MvcInstaller.
For the MvcInstaller, the following requirements should be met prior to running the installer for the first time:
1. Create the database, and make sure to create the db login account to be used in accessing this database.
2. Modify the installer.config file, example is as follows:
5. Make sure to select Use SecurityGuard, then click Install.
6. After a few seconds, it should display a message that the installer completed successfully.
7. Check the database to make sure that the tables and users have been created, in SQL Server Management Studio, it should look like this:
8. Finally, go to /SecurityGuard/Membership/Index, this is what you should see:
Success! Now we have an instant membership management for our app.
Issues encountered during implementation and deployment:
1. When I deployed this to the production server, I got an error like below because the installer.config and web.config are inaccessible:
2. Another issue that I encountered is that because the application is intended for the intranet and I'm using windows integrated security, there is a problem when using an account preceded by a domain name such as DOM1\sammydc, the backslash causes some issues with the UI and routing logic. I will write up the solution to this problem on my next post.
To install via NuGet:
PM> Install-Package SecurityGuard.MVC4To make it easier to create and setup the Membership database tables and roles, I recommend to use another Nuget package called MvcInstaller.MVC4:
PM> Install-Package MvcInstaller.MVC4Follow these steps to setup SecurityGuard.
Follow these steps to setup MvcInstaller.
For the MvcInstaller, the following requirements should be met prior to running the installer for the first time:
1. Create the database, and make sure to create the db login account to be used in accessing this database.
2. Modify the installer.config file, example is as follows:
<installerconfig> <applicationname>ProductCoC</applicationname> <path> <relativesqlpath>App_Data</relativesqlpath> </path> <membership create="true" providername="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <profile providername="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <rolemanager providername="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <roles> <role name="Administrator"> <users> <user email="sammydc@mail.com" password="ProductCoC" secretanswer="Green" secretquestion="Favorite Color" username="WBI\sammydc"> </user></users> </role> <role name="SecurityGuard"> <users> <user email="sammydc@mail.com" password="ProductCoC" secretanswer="Green" secretquestion="Favorite Color" username="WBI\sammydc"> </user></users> </role> </roles> </rolemanager> <sessionstate providername="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <database usetrustedconnection="false"> <connectionstringname>MembershipConnection</connectionstringname> <datasource>MSSQ4\Instance1</datasource> <initialcatalog>ProductCoC</initialcatalog> <username>ProductCoC</username> <password>ProductCoC</password> </database> </sessionstate></profile></membership></installerconfig>3. Make sure that the AppInstalled setting in web.config is set to false
<appsettings> <add key="AppInstalled" value="false" /> </appsettings>4. Now deploy the MVC 4 application. After deployment, from the browser, navigate to the install path, you should see this on the browser:
5. Make sure to select Use SecurityGuard, then click Install.
6. After a few seconds, it should display a message that the installer completed successfully.
7. Check the database to make sure that the tables and users have been created, in SQL Server Management Studio, it should look like this:
8. Finally, go to /SecurityGuard/Membership/Index, this is what you should see:
Success! Now we have an instant membership management for our app.
Issues encountered during implementation and deployment:
1. When I deployed this to the production server, I got an error like below because the installer.config and web.config are inaccessible:
To overcome this issue, I needed to temporarily add, the Everyone group in windows to have modify access to these files. Make sure to change back the permissions right after the setup.
2. Another issue that I encountered is that because the application is intended for the intranet and I'm using windows integrated security, there is a problem when using an account preceded by a domain name such as DOM1\sammydc, the backslash causes some issues with the UI and routing logic. I will write up the solution to this problem on my next post.
3.07.2013
Syntax Highlighter for Blogger using Prettyfy
Adding Prettify Syntax Highlighter to any CMS or webpage is very easy. For Blogger you just need to go to your blog’s Template tab, click “Edit HTML” button and add the below script before </head> tag.
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js">As you can see that the project is hosted on Google Code. This is an auto-loader which will automatically load the required JavaScript API and the CSS file from the subfolders of SVN. So the last thing that you are left is to mention where it should highlight the codes by adding class="prettyprint" to pre and/or code tags within Blogger Post Editor, HTML view. Here is an example:
<pre class="prettyprint">
$(function() {
alert('I am using Prettify as Syntax Highlighter');
});
</pre>
There are also some styles available that you can call and load the stylesheet in one URL, here is an example:
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?skin=sunburst"></script>
2.27.2013
AraNight - My Color Scheme for Visual Studio
I have been changing my color scheme for Visual Studio. I have decided to share it at...
http://studiostyl.es/schemes/aranight
http://studiostyl.es/schemes/aranight
Subscribe to:
Posts (Atom)