• Microsoft

    C# Full Timestamp

    Because I’m always having to look this up… a full (useful) timestamp should always look like this…

    YYYYMMDD HH:MM:SS.fff   => 20200730 14:58:00.000   

    string timeStamp = DateTime.Now.ToString("yyyyMMdd hh:mm:ss.fff tt");  //Example 20200730 03:02:39.591 PM
    string fileTimeStamp = DateTime.Now.ToString("yyyyMMdd_HHmmssfff");    //20200730_150535040
    string fileTimeStamp2 = DateTime.Now.ToString("yyyyMMdd_hhmmssffftt");// 20200730_030707929PM

     

    ~SG

     

  • Blazor,  Microsoft

    Blazor: And the Identity Scaffolding Mess

    Ok, back around to the annoying bin. Let’s talk adding Identity to a Blazor Web Assembly app…

    Out of the box when you start with a Blazor WebAss project using Identity and of course ASP Core hosted (I’m using preview 2). You then decide that all of the hidden razor pages really need to be customized (don’t even get me started on Hidden razor pages). Well there is a way to do just that.

    You Add Scaffolded Item to the Server project and select Identity (nope not going to show you that- there’s plenty of how-to on the internet). I wait while you go figure out how-to do that……   Wait  wait wait wait….

    All right, now you have those missing identity pages  up in Areas/Identity/Pages/Account – and you have an app that compiles and run, right? Well…. NO you don’t. Because now your app is broken… And that’s where we start… Let’s fix it.

    The Fix –

      RegisterConfirmation.chtml.cs  (if during your scaffold you selected the RegisterConfirmation page to include in the Areas/Identity/Pages/Account)
        You will need to add the reference using Microsoft.AspNetCore.Identity; – this is missing from the code behind.

      The Scaffold also creates an another wwwroot folder in the .Server project – The app runs into conflicts with css etc… 
        All you need to do here is delete the wwwroot folder from the .Server project. The Blazor will come up normal.

    Now, there maybe other compile errors depending on the mix of .dot core you have, etc. You will just need to work through those.

    Another annoying thing that happens when you mix Blazor and these older razor identity pages… is how the identity system works when you logout. Normally one likes to have the app navigate back to the home page when one logs out. This is doable but not in the box on the template.

    This is easy fix….

    In your client project in the Pages folder, you have a Authentication.razor file:

    @page "/authentication/{action}"
    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    <RemoteAuthenticatorView Action="@Action" />
    
    @code{
        [Parameter] public string Action { get; set; }
    }

    This is the default page… Let’s make a change…

    @page "/authentication/{action}"
    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    @inject NavigationManager navMGR
    <RemoteAuthenticatorView Action="@Action">
        <LogOutSucceeded>
          @{navMGR.NavigateTo("/");}
        </LogOutSucceeded>
    </RemoteAuthenticatorView>
    
    @code{
        [Parameter] public string Action { get; set; }
    }

    We injected the NavigationManager, because we want to navigate to the home page. 
     One see that in the RemoteAuthenticationView we have some options… LogOutSucceeded is a component option that allows us to do something, when the user has logged out with no errors. 
     This is good place to put a NavigateTo Blazor command… and as we see we can force the app (from the box) to go to the home page.

    Neat… 

    Now yes, the errors and problems are annoying… they have been for the scaffold for awhile now… but hopefully as Blazor moves out of preview, these things will be fixed.

    ~ScottGeek   Happy Blazoring….

     

     

     

     

     

  • .Net Core,  Microsoft

    Play with dotnet tool and a BOT

    I was poking about with cleaning up my DotNet Core sdks and in my travels a found this thing call dotnet tools… Let the experiment begin… To credit the source of an article on Microsoft go here: dotnet tool 

    Do a console project with dotnet:

    dotnet new console -o botsay

    Then, of course, cd into the botsay directory… and edit the Program.cs (yeap you can do this in VS 2017)

    Here’s the main method:

    using System.Reflection;

    static void Main(string[] args)
    {
    if (args.Length == 0)
    {
    var versionString = Assembly.GetEntryAssembly()
    .GetCustomAttribute&lt;AssemblyInformationalVersionAttribute&gt;()
    .InformationalVersion
    .ToString();

    Console.WriteLine($"botsay v{versionString}");
    Console.WriteLine("-------------");
    Console.WriteLine("\nUsage:");
    Console.WriteLine(" botsay &lt;message&gt;");
    return;
    }

    ShowBot(string.Join(' ', args));
    }

    The ShowBot method is this:

    static void ShowBot(string message)
    {
        string bot = $"\n        {message}";
        bot += @"
        __________________
                          \
                           \
                              ....
                              ....'
                               ....
                            ..........
                        .............'..'..
                     ................'..'.....
                   .......'..........'..'..'....
                  ........'..........'..'..'.....
                 .'....'..'..........'..'.......'.
                 .'..................'...   ......
                 .  ......'.........         .....
                 .    _            __        ......
                ..    #            ##        ......
               ....       .                 .......
               ......  .......          ............
                ................  ......................
                ........................'................
               ......................'..'......    .......
            .........................'..'.....       .......
         ........    ..'.............'..'....      ..........
       ..'..'...      ...............'.......      ..........
      ...'......     ...... ..........  ......         .......
     ...........   .......              ........        ......
    .......        '...'.'.              '.'.'.'         ....
    .......       .....'..               ..'.....
       ..       ..........               ..'........
              ............               ..............
             .............               '..............
            ...........'..              .'.'............
           ...............              .'.'.............
          .............'..               ..'..'...........
          ...............                 .'..............
           .........                        ..............
            .....
    ";
        Console.WriteLine(bot);
    }

    It’s a simple core console app that takes a text phrase as input and shows the bot saying it.

    Now the fun part… Of course, you should make sure that the code compiles. 
    Go back to your command window and do the folllowing:

     

    dotnet run
    dotnet run -- "Hello from the bot"
    dotnet run -- hello from the bot

    It’s a Bot!

    Now… let’s make a dotnet tool…

    Edit your botsay.csproj file. You can do this in VS 2017 or your favorite notepad… 

    You need to add these lines:

      &lt;PackAsTool&gt;true&lt;/PackAsTool&gt;
    &lt;ToolCommandName&gt;botsay&lt;/ToolCommandName&gt;
    &lt;PackageOutputPath&gt;./nupkg&lt;/PackageOutputPath&gt;
     

    In the end the csproj file show look like:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
    
        <PackAsTool>true</PackAsTool>
        <ToolCommandName>botsay</ToolCommandName>
        <PackageOutputPath>./nupkg</PackageOutputPath>
    
      </PropertyGroup>
    
    </Project>

    Now the tool making part:

    dotnet pack
    
    dotnet tool install --global --add-source ./nupkg botsay
    
    You can invoke the tool using the following command: botsay
    Tool 'botsay' (version '1.0.0') was successfully installed.

    Once you get the message back from the “dotnet tool”… command you now have a dotnet tool…
    ** You may have to close the command window and open a new one… depending on your OS, etc.

    You can type botsay –You’re Message Here…

    Now, as you experiment don’t forget to clean up 

    dotnet tool uninstall -g botsay

    You should read more about dotnet tool… start here: dotnet tool on Mircosoft docs