Blazor Server website with Azure Identification - blazor-server-side

I'm spinning around and around trying to find a comprehensive description on how to upgrade a Blazor server website without authentication to use the latest MSAL authentication and not ADAL which is going to be dropped in 2022.
The issue I had are that there are bit and pieces here and there and finally I got it to compile and start up, but when the website gives me a choice of the login since I have multiple ad connections, it repeats that choice after I select one. It never gets to complete the handshake in some form.
Here is what I have:
Startup.cs in ConfigurationService:
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd");
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddMicrosoftIdentityUI();
Appsettings is:
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "zzzzzzzzz.onmicrosoft.com",
"TenantId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ClientId": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
"CallbackPath": "/signin-oidc"
},
LaunchSettings:
"ITraxBlazer": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
Packages:
<ItemGroup>
<PackageReference Include="Blazored.Toast" Version="3.1.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.5.1" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.5.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="Telerik.Documents.Spreadsheet" Version="2021.1.118" />
<PackageReference Include="Telerik.UI.for.Blazor" Version="2.21.0" />
</ItemGroup>
Thanks a bunch for any help!

The issue was that I didn't have the two indented lines here in StartUp.cs
app.UseRounting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints();

Related

net::ERR_CONNECTION_REFUSED on local IIS site when trying to hit API

I have an application with a VueJS frontend and .netcore backend. I am trying to deploy the site to an Azure VM 2019 windows server.
I used this guide to setup the website on IIS:
https://jasonwatmore.com/post/2020/01/11/vuejs-aspnet-core-on-azure-with-sql-server-how-to-deploy-a-full-stack-app-to-microsoft-azure
The following web.config is probably the most relevant from that guide
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Vue" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^/api/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
My endpoints look like this: [HttpGet("/api/player/all")]
I have a Main.js in my Vue side that sets up axios to use port 44371
const axiosConfig = {
baseURL: 'https://localhost:44371',
};
Vue.prototype.$axios = axios.create(axiosConfig)
And finally my launchsettings
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60000",
"sslPort": 44371
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Service": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:60000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
So far I haven't been able to get a good error message, everything works fine when trying this on my local machine. I feel like I have some port or path incorrectly configured. Thanks.

.csproj bug in Asp .NET Core MVC

I have wanted to create new table in my database (SQLite), so I created new Model and used it in ApplicationDbContext class like this:
public DbSet<AppUser> AppUsers { get; set; }
public DbSet<Report> Reports { get; set; }
Then executed the command later:
dotnet ef migrations add Report
I can see migration file.
In Startup.cs file I have:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(connectionString));
In EFRepository.cs:
public IQueryable<AppUser> AppUsers
=> ctx.AppUsers;
public IQueryable<Report> Reports => ctx.Reports;
After this somehow suddenly I received many errors connected with this file ( .csproj):
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>Mushrooms</AssemblyName>
<RootNamespace>Mushrooms</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
I checked dotnet --info command, but I can't see incompatibility.
Three of the errors:
Invalid markup declaration '<'
Invalid markup declaration '
Invalid token "" at root level of document.
and many more in every line of this file.
This errors have occured when system was starting up.
I am using Asp .NET Core MVC 3.0.0.
Before this everything was working fine, any suggestions?
I try to create blank .net core 3.0 project and this csproj file is working fine
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
</ItemGroup>
</Project>
Try to check your project extension file should be .csproj

Pomelo.EntityFrameworkCore.Mysql error DBContextOptionsBuilder does not contain a definition for UseMyQL

I just installed Visual Studio 17 and I want to use mysql as my database to develop a WebAPI.
My csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
From NuGet package manager I installed Pomelo.EntityFrameworkCore.Mysql.
My aspsettings.json:
{
"ConnectionStrings": {
"MysqlConnection": "server=localhost;userid=root;pwd=root;port=3306;database=aspnet;sslmode=none;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
In my strtup.cs inside ConfigureServices() I have:
services.AddDbContext<WebAPIDataContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("MysqlConnection")); }
);
services.AddMvc();
services.AddScoped<IProfileRepository, ProfileRepository>();
However, it is giving me DBContextOptionsBuilder does not contain a definition for UseMyQL error. Why is it that?
I changed it to:
// Add framework services.
services.AddDbContext<WebAPIDataContext>(options =>
{
options.UseMySql(Configuration.GetConnectionString("MysqlConnection"));
});
in Stertup.cs & appsettings.json & DbContext :
services.AddDbContext<mvccoreContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")
));
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=mvccore;User=root;Password=;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseMySql("");
}
}

error page handling not apper on asp.net core

is there any problem with error page on browser ?, just when i hit http://localhost:5000/invalid, the error info only appear on cli
this is my startup.cs files
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
env.EnvironmentName = EnvironmentName.Production;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
if(context.Request.Path.Value.Contains("invalid"))
throw new Exception("ERROR!");
await next();
});
app.UseFileServer();
}
}
}
and this is my .csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
</ItemGroup>
</Project>

xUnit configuration for ASP.NET Core 1.1 MVC in Visual Studio 2017 RC

Unit testing ASP.NET Core MVC web apps in Visual Studio 15, I could put a using statement for Microsoft.AspNetCore.Mvc in my test file and then access framework classes, such as ViewResult. Using Visual Studio 2017 RC, I cannot even locate Microsoft.AspNetCore.Mvc in the test project. I believe it must be to do with dependencies.
The project.json configuration from VS15:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
},
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"moq": "4.6.38-alpha",
"System.Diagnostics.TraceSource": "4.3.0",
"SportsStore": "1.0.0",
"xunit": "2.1.0",
"Microsoft.DotNet.InternalAbstractions": "1.0.0" // Required for xUnit with NetCore 1.1
},
"frameworks": {
"netcoreapp1.1": {
"imports": [ "dotnet5.6", "portable-net45+win8" ]
}
}
}
The SportsStore.Test.csproj configuration from VS17:
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.InternalAbstractions">
<Version>1.0.500-preview2-1-003177</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk">
<Version>15.0.0-preview-20161123-03</Version>
</PackageReference>
<PackageReference Include="Moq">
<Version>4.6.38-alpha</Version>
</PackageReference>
<PackageReference Include="System.Diagnostics.TraceSource">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="xunit">
<Version>2.2.0-beta4-build3444</Version>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio">
<Version>2.2.0-beta4-build1194</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SportsStore\SportsStore.csproj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
The VS17 project uses xunit.runner.visualstudio rather than dotnet-test-xunit due to latter only being compatible with project.json, not .csproj. I suspect this dependency change is the cause of my problem. How do I correct this and get access to the MVC framework?
Hmm. I should always remember the helpful IT support advice... "Have you tried restarting it?"
Seems to have fixed the problem. I've reported it as a potential bug since I don't think the IDE should need restarting when adding a test project to the solution.