asp.net core - what are the differences of these MapRazorPages() calls? - asp.net-core

Method 1
In ASP.NET Core 3.x, we add endpoints for Razor Pages with
app.UseEndpoints(endpoint =>
{
endpoint.MapRazorPages();
});
Method 2
In the ASP.NET Core 6 Web App template (minimal hosting model), this has been changed to:
app.MapRazorPages();
From MS docs page:
Apps typically don't need to call UseRouting or UseEndpoints. WebApplicationBuilder configures a middleware pipeline that wraps middleware added in Program.cs with UseRouting and UseEndpoints. For more information, see Routing in ASP.NET Core.
My questions are:
Do the 2 method calls above have the same effect?
Am I correct to assume that the second way (i.e. app.MapRazorPages()) is possible due to the middleware pipeline configuration by WebApplicationBuilder? (see snippet from MS docs above).
Thank you in advance.

Short answer: Yes. They are using the same method.
Details you could see below explain:
endpoint.MapRazorPages():
app.MapRazorPages();:

Related

Magic link: OpenIdConnectAuthenticationHandler: message.State is null or empty

Reference:
https://github.com/azure-ad-b2c/samples/tree/master/policies/sign-in-with-magic-link
Using this in your Production Application - Could you please help how can we use authentication library to create a state and handle authentication request using id_token_hint.
It has been explained in this referenced link. Please help if there is any link how we can integrate magic link custom policy with production app. Note: using run as link and jwt.ms it is working as expected.
I am using Asp.net core 3.1 and c# programming language. I was following web api which is mentioned in this doc (source code) . I added another controller, model and view with top of this api(which has identity controller and oidc controller).
This can be achieved by passing id_token_hint to account controller.

Sample or tutorial to use Microsoft.AspNet.Identity.Core.<culture> for Identity Localization?

I need to localize (in italian) all the pages of Identity/Account Area that I scaffolded (I started the asp.net 3.1 core project with No Authentication and then added the pages below with the Add New Scaffolded Item...)
I discovered there is for every culture an assembly Microsoft.AspNet.Identity.Core. (eg. Microsoft.AspNet.Identity.Core.it), but I don't understood how to use.
I tried to load and reference it, but nothing changes... do you have a sample how to use?
Is it usable only if I use the "hidden" mode (eg. create the asp.net 3.1 project with Authentication Individual User Account)?
Thanks
Sandro
\Areas\Identity\Pages\Account\AccessDenied.cshtml.cs
\Areas\Identity\Pages\Account\ConfirmEmail.cshtml.cs
\Areas\Identity\Pages\Account\ConfirmEmailChange.cshtml.cs
\Areas\Identity\Pages\Account\ExternalLogin.cshtml.cs
\Areas\Identity\Pages\Account\ForgotPassword.cshtml.cs
\Areas\Identity\Pages\Account\ForgotPasswordConfirmation.cshtml.cs
\Areas\Identity\Pages\Account\Lockout.cshtml.cs
\Areas\Identity\Pages\Account\Login.cshtml.cs
\Areas\Identity\Pages\Account\LoginWith2fa.cshtml.cs
\Areas\Identity\Pages\Account\LoginWithRecoveryCode.cshtml.cs
\Areas\Identity\Pages\Account\Logout.cshtml.cs
\Areas\Identity\Pages\Account\Register.cshtml.cs
\Areas\Identity\Pages\Account\RegisterConfirmation.cshtml.cs
\Areas\Identity\Pages\Account\ResendEmailConfirmation.cshtml.cs
\Areas\Identity\Pages\Account\ResetPassword.cshtml.cs
\Areas\Identity\Pages\Account\ResetPasswordConfirmation.cshtml.cs
\Areas\Identity\Pages\Account\Manage\ChangePassword.cshtml.cs
\Areas\Identity\Pages\Account\Manage\DeletePersonalData.cshtml.cs
\Areas\Identity\Pages\Account\Manage\Disable2fa.cshtml.cs
\Areas\Identity\Pages\Account\Manage\DownloadPersonalData.cshtml.cs
\Areas\Identity\Pages\Account\Manage\Email.cshtml.cs
\Areas\Identity\Pages\Account\Manage\EnableAuthenticator.cshtml.cs
\Areas\Identity\Pages\Account\Manage\ExternalLogins.cshtml.cs
\Areas\Identity\Pages\Account\Manage\GenerateRecoveryCodes.cshtml.cs
\Areas\Identity\Pages\Account\Manage\Index.cshtml.cs
\Areas\Identity\Pages\Account\Manage\ManageNavPages.cs
\Areas\Identity\Pages\Account\Manage\PersonalData.cshtml.cs
\Areas\Identity\Pages\Account\Manage\ResetAuthenticator.cshtml.cs
\Areas\Identity\Pages\Account\Manage\SetPassword.cshtml.cs
\Areas\Identity\Pages\Account\Manage\ShowRecoveryCodes.cshtml.cs
\Areas\Identity\Pages\Account\Manage\TwoFactorAuthentication.cshtml.cs
``
I tried to load and reference it, but nothing changes... do you have a sample how to use?
Is it usable only if I use the "hidden" mode (eg. create the asp.net 3.1 project with Authentication Individual User Account)?
As far as I know, the Microsoft.AspNet.Identity.Core is used for ASP.NET not for ASP.NET Core. The asp.net core identity package is Microsoft.AspNetCore.Identity. There is no simple to use asp.net identity in asp.net core.
If you want to use Localization for asp.net core identity, you should have basic knowledge of Localization for asp.net core. Please refer to this article. You should know what is the Resource files and know how it work in asp.net core to generate different language for different culture.
Then you could refer to this example project which has already enabled Localization for the asp.net core identity.
You could directly Fork the repro, create resource files for the language you want to translate, add the new language to the supportedCultures list before zu-ZA in Startup.cs and pull a request to test.
Notice: This example project is 2.2. If you want to use 3.1, you could follow the example project to create Localization for 3.1 project.

ASP NET Core Web API returning a link to an image failing

I have a working ASP.NET Web API. I am currently converting it to ASP.NET Core 3.1.
In one of the API calls, I check if an image is present in a folder. If it is not, I create it (in code) and then send a link to it (amongst other things) back to the caller. The check if exists and creation of the image works fine in ASP.NET Core, but when I send back the link to the image, the client does not find it and hence it appears as a broken link on their web page.
See this diagram for the structure and for example the EnabledTRImage.png file.
The Url I am returning is:
http://localhost:59682/TR/128/EnabledTRImage.png
I have also tried returning:
http://localhost:59682/wwwroot/TR/128/EnabledTRImage.png
But this fails too.
One thing I have noticed is that in ASP.NET Core, creating the image in code makes it part of the Project, whereas in ASP.NET it does not. I was wondering if that had anything to do with the issue.
Unfortunately, these images are central to the Web API. So I am stuck.
Any ideas? Thanks
I just fixed it myself. I had not specified:
app.UseStaticFiles();
In the Startup/Configure method. That line is not there in the WebApi template (as expected I guess)
In the Startup/Configure method.
Instead of app.UseStaticFiles();
Specify your folder path in StaticFileOptions()
app.UseStaticFiles(new StaticFileOptions(){
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), #"ProductImagesFiles")),
RequestPath = new PathString("/ProductImagesFiles")
});
'

Any alternatives to aspnet-request:serverVariable when using NLog with .Net Core?

As stated on the NLog GitHub the ${aspnet-request:serverVariable=String} layout renderer is not supported in .Net Core.
The documentation doesn't provide alternatives to many of the variables available under serverVariable.
My question is, are there any alternatives? Like to access remote address, server name, port etc? Or do I just have to write a bunch of custom layout renderers documented here and dependency inject all the stuff by hand?
For ASP.NET Core there as many new layout renders. The reason is that the API of ASP.NET Core is very different and the server variables can't be read like in ASP.NET (so non-core)
There are currently 13 layout renders for ASP.NET Core that renders a part of the request.
${aspnet-request} - ASP.NET Request variable.
${aspnet-request-contenttype} - ASP.NET Content-Type header (Ex. application/json)
${aspnet-request-cookie} - ASP.NET Request cookie content.
${aspnet-request-form} - ASP.NET Request form content.
${aspnet-request-headers} - ASP.NET Header key/value pairs.
${aspnet-request-host} - ASP.NET Request host
${aspnet-request-ip} - Client IP.
${aspnet-request-method} - ASP.NET Request method (GET, POST etc).
${aspnet-request-posted-body} - ASP.NET posted body / payload
${aspnet-request-querystring} - ASP.NET Request querystring.
${aspnet-request-referrer} - ASP.NET Request referrer.
${aspnet-request-url} - ASP.NET Request URL.
${aspnet-request-useragent} - ASP.NET Request useragent.
See also https://nlog-project.org/config/?tab=layout-renderers&search=package:nlog.web.aspnetcore
If you need something else, you could indeed create a custom renderer. If you need the http request you could use:
AspNetLayoutRendererBase.Register("aspnet-request-myrenderer", (logevent, httpcontext, config) => ... );
You need to reference the NLog.Web.AspNetCore package for that.

Autobinding in MVC4 WebApi with Ninject.Extensions.Conventions

In my MVC4 app I'm using Ninject.Extensions.Conventions to autobind all itnerfaces with their implementation using default mechanism:
kernel.Bind(x => x
.FromAssembliesMatching("*")
.SelectAllClasses()
.BindDefaultInterface());
This works great for regular controllers, but doesn't for WebApi controllers. What do I need to change/add?
Ok, I resolved the issue by following this article:
http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
In short: I created my own dependency resolver (as per article) and assigned it to GlobalConfiguration.Configuration.DependencyResolver as suggested by nemesv
There are already a many examples on how to integrate NInject with Web API through the web, using:
DependencyResolver
On ASP.NET website is shown standart way of integration of any DI/IoC with Web API:
http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
IDependencyResolver.
WebApiContrib project shows how to do it with IDependencyResolver
https://github.com/WebApiContrib/WebApiContrib.IoC.Ninject
Please post more information about your implementation of these resolvers, to be more specific in answer.