0 Comments

Small reminder how to fake User.Identity.IsAuthenticated  and User.Identity.Name  inside unit tests, while testing controller code. It’s not so obvious and I often forget this small trick, which is also important when writing user authentication code (login).

Our simple controller returns a model, where one property depends on authenticated user state:

In unit test we want to validate model properties, specially CanWriteStory which has to be True if user is authenticated.

Simple unit test for this controller:

This test will pass (User.Identity.IsAuthenticated will be True) because we configured authentication type “someAuthTypeName”  (here and here)! Without looking at MVC source code it would take me a long time to figure that out, and to be honest correlation between IsAuthenticated and AuthenticationType is still little bit confusing. 

The test also shows how simple it is to set HttpContext to controller, without need of using some mocking framework. If I’m brave enough, I would try to write similar test for WebForms and Page class, but I’m notSmile

Same rule, with setting of authentication type applies when writing custom login code, which looks probably something like this:

var claims = new List<Claim> { new Claim(ClaimTypes.Name, user.Result.Username) };

await _httpContext.HttpContext.Authentication.SignInAsync(Application.AuthScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, "form")));

if we forget to set auth type “form” (can be whatever), user will not be authenticated!

0 Comments

image

If you try to install latest .NET Core SDK 1.0.1 on Mint 18 or Elementary OS Loki (based on Ubuntu 14.04 and 16.04 LTS) by following instructions on http://get.asp.net, you might see this error:

mint-vbox hudo # sudo apt-get install dotnet-dev-1.0.0-preview2-003131
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
dotnet-dev-1.0.0-preview2-003131 : Depends: dotnet-sharedframework-microsoft.netcore.app-1.0.1 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

dotnet-sharedframework-microsoft.netcore.app-1.0.1 is missing, but if you try to apt-get that, then dependent libicu52 is missing, only libicu55 is in Ubuntu repository.

We can get it from this Url, by downloading .deb file: https://launchpad.net/ubuntu/wily/amd64/libicu52/52.1-8 (you’ll probably choose Arm64 release binary)

Since we can’t just execute this .deb by double-clicking on it (at least on Elementary OS), we have to move it to repo cache and use apt-get install, or better, use gdebi utility (Mint 18 already includes it):

sudo apt-get install gdebi

If we double-click to downloaded .deb file now, a window will appear offering:

image

After that, executing last .net core installation step takes us to the happy place Smile :

mint-vbox hudo # sudo apt-get install dotnet-dev-1.0.0-preview2-003131
Reading package lists... Done
….

Setting up dotnet-sharedframework-microsoft.netcore.app-1.0.1 (1.0.1-1) ...
Setting up dotnet-dev-1.0.0-preview2-003131 (1.0.0-preview2-003131-1) ...
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit
http://aka.ms/dotnet-cli-eula for more information.
Processing triggers for libc-bin (2.23-0ubuntu4) ...
hudo-VirtualBox hudo # dotnet

Microsoft .NET Core Shared Framework Host

  Version  : 1.0.1
  Build    : cee57bf6c981237d80aa1631cfe83cb9ba329f12

Usage: dotnet [common-options] [[options] path-to-application]

Common Options:
  --help                           Display .NET Core Shared Framework Host help.
  --version                        Display .NET Core Shared Framework Host version.

Options:
  --fx-version <version>           Version of the installed Shared Framework to use to run the application.
  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.

Path to Application:
  The path to a .NET Core managed application, dll or exe file to execute.

If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.

To get started on developing applications for .NET Core, install .NET SDK from:
 
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
mint-vbox hudo #

Visual Studio Code and Omnisharp support

Since elementary OS 0.4 Loki, platform Id changed from “elementary OS” to “elementary”. To fix Omnisharp problem of not recognizing  this version of OS, edit platform.js  file in /home/username/.vscode/extensions/ms-vscode.csharp-1.4.1/out/  by changing line (I removed “OS” in line 76):

case 'elementary':

on mint, this case branch needs to be added to the end of switch statement:

case 'linuxmint':
    var versionId = getValue("VERSION_ID");
    if (versionId.startsWith("17")) {
        // This also works for Linux Mint
        return Platform.Ubuntu14;
    }
    else if (versionId.startsWith("18")) {
        return Platform.Ubuntu16;
    }
    break;

Don’t forget to restart VS Code after changing platform.js. Hopefully, VS Code and Omnisharp will be able to recognize the project, install necessary files, like build tasks and debugger support!