Showing posts with label VS. Show all posts
Showing posts with label VS. Show all posts

Mar 13, 2020

Strong Name Signing in .NET

Using a strong name protects your assembly against manipulation.

For example your software using a class named Class1 in ClassLibrary1 namespace.

If you compile a new dll contains ClassLibrary1.Class1 then you can use this dll instead of original one.

This is very basic example but it can be applicable in real life.

To be sure that your software is using your assembly version then you can sign your project.

Microsoft description is:
When a strong-named assembly is created, it contains the simple text name of the assembly, the version number, optional culture information, a digital signature, and the public key that corresponds to the private key used for signing.
Example here shows signing process in Visual Studio. You can use also command line tool (sn.exe).

Coding

Our solution has 2 projects, executable and a class library. Main program is using ClassLibrary1.Class1

using ClassLibrary1;
using System;

namespace strongname
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("strongname loaded");

            Class1 c = new Class1();

            Console.ReadKey();
        }
    }
}


using System;

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
            Console.WriteLine("Hello from Class1");
        }
    }
}

Sign

Right click to project in VS and select Properties. In Signing tab check "Sign the assembly" and select "New" in listbox. Enter password for your private key.


This will create a pfx (Personal Information Exchange Format) file in your project folder.

Run

strongname loaded
Hello from Class1

Recompile

If you create a new project with the same namespace, class and functions then you can use the new dll with current executable.

For test change the code and recompile dll in outside of project folder.

csc.exe /t:library /out:ClassLibrary1.dll Class1.cs
Then replace the old ClassLibrary1.dll with the new file.

Result

When you try to run your executable an error will be shown because the new dll has not signed with correct key.

C:\strongname\bin\Debug\strongname.exe

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=77d705841860f240' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at strongname.Program.Main(String[] args)

If you completely remove signing from your projects then your manipulated dll will work.

Jan 7, 2020

Asp.Net Settings for Hosting

1. Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request failed.

Solution:

<system.web>
   <trust level="Full" />
</system.web>

2. Publish website without roslyn

Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform "Your Web API project name"
In your publish profile settings, uncheck "Allow precompiled site to be updatable". You can find this under Settings > Precompile during publishing > configure.

Remove codedom from web.config.
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
    </compilers>
  </system.codedom>

4. MapPath

  System.Web.Hosting.HostingEnvironment.MapPath("~/log");

4. Restrict file download

Create web.config file in the folder you want to prevent file download for a specific extension.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <fileExtensions applyToWebDAV="false">
                     <add fileExtension=".txt" allowed="false" />
                </fileExtensions>
            </requestFiltering>
        </security>
  </system.webServer>
</configuration>

Dec 4, 2018

Using git (VS 2017 edition) with VSCode

Make sure the folder contains git.exe is in PATH in Windows.

For Visual Studio Community Edition git.exe is in:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd

Add project to local git

cd "folder path to your repo"
git init
git add . # add everything
git commit -m "initial commit" 

Use source control tab in VS Code

If you don't see GIT in source control tab you can update your git.exe location.

  1. Go to settings in VSCode and search for "git path".
  2. Click "Edit in settings.json"
  3. Add path to user settings

"git.path": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\Git\\cmd\\git.exe"

Add to remote repository

C:\repo>git config credential.helper store

C:\repo>git push https://gitlab.com/username/repo.git --all
git: 'remote-https' is not a git command. See 'git --help'.
git-remote-https command is not reachable
C:\repo>git-remote-https
'git-remote-https' is not recognized as an internal or external command,
operable program or batch file.
Add git-remote-https.exe folder path to PATH.
C:\repo>SET PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\bin
Now it is reachable.
C:\repo> git-remote-https                                                                                                      
error: remote-curl: usage: git remote-curl  []
C:\repo>git push https://gitlab.com/username/repo.git
git: 'credential-cache' is not a git command. See 'git --help'.
Username for 'https://gitlab.com': username
Password for 'https://username@gitlab.com':
git: 'credential-cache' is not a git command. See 'git --help'.
Counting objects: 2653, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2324/2324), done.
Writing objects: 100% (2653/2653), 23.62 MiB | 170.00 KiB/s, done.
Total 2653 (delta 670), reused 0 (delta 0)
remote: Resolving deltas: 100% (670/670), done.
To https://gitlab.com/username/repo.git
 * [new branch]      master -> master
Error: 'credential-cache' is not a git command

For msysgit versions 1.8.1 and above
git config --global credential.helper wincred
Versions older than 1.8.1
git config --global credential.helper winstore
Git config file:
[credential]
 helper = winstore
Related errors:
Git http-push is not a git-command
Adding "..\mingw32\bin" folder also solves it.

VSCode commit

Now you should be able to commit and push to remote repository in VSCode. After any change go to source control tab and press "Stage Changes" button. Fill the message and press Commit (check icon) button.

Apr 22, 2012

Rosyln

We saw many improvements, new libraries, new user interfaces in Visual Studio in years. But it is the first time that i heard about a compiler development. Microsoft is working on dynamic compiling technology - called Rosyln.

Current compiler depends on C++ infrastructure. New compiler will allow us to compile the code on .Net framework. I couldn't see any information about the compile time issue. It may be faster or not. But this is not the question yet. There are questions about this technology in my mind. What can we do with Roslyn?
  • First thing is that bringing an idea to me about Visual Studio. Since the new compiler seems to be a service, a Visual Studio version may be just an editor on our computers. We may write code on different editors then check and build our code on the server. This will allow us to write code on the web interface. Even on MAC we will be able to write code. Because the compiler doesn't need to be on our client. Such a this scenario is mostly applicable on the web applications i think.
  • Second thing is the security concerns. When we say dynamic compiling, it includes an input can create an assembly. How will we know someone write some code to run dynamically when we run an applicaiton?  The application can run a code that is saved in a file like this: scriptEngine.ExecuteFile("script.csx");
Microsoft's new technology will bring new and useful features. As far as i know it is published as a CTP release. In the future we will see the real life examples about it.