I'm trying to configure IdentityServer4 in a .Net Core 3 project.
My ConfigureServices-Method looks like this:
services.AddIdentityServer()
.AddSigningCredential(new X509Certificate2(#"*redacted*", "*redacted*"))
.AddTestUsers(InMemoryConfiguration.GetUsers().ToList())
.AddConfigurationStore(builder => builder.UseSqlServer(...))
Unfortunately, the builder.UseSqlServer(...) which used to work in earlier versions is not available anymore.
What is the correct way to configure IdentityServer4 to use an SQL Server nowadays?
Thanks in advance
Try this
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b =>
b.UseSqlServer(...);
})
Related
I’m unable to get data from .Net 6 API into Blazor WASM app.
Firefox shows the on the bottom the very often mentioned error:
I worked on this tutorial:
https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-6.0&pivots=webassembly
To fix the CORS Problem I read this document:
https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0
I have no idea what I doing wrong. Please can someone have I look in my demo app on GitHub? I created a simple demo app.
https://github.com/Christoph1972/BlazorTestGetAPIData
for CORS you can do this:
options.AddPolicy("CorsPolicy", builder => builder.WithOrigins("http://<YOUR_URL>")//The client Url that wants to get data
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
and
var app = builder.Build();
app.UseHttpsRedirection();
app.UseCors("CorsPolicy"); <-----
I'm developing a cross-platform (win/mac/linux) application and I'm looking to store my application state. What is the .NET Core recommended way of doing this?
I've dug through the documentation and the closest thing I found was this, which is aimed at Visual Studio/.NET Framework (and I'm on VS Code).
There are 3 ways,
ONLY For Localhost
Simply stash them in your appsettings.json or as environment
variables.
For Staging / Production,
Azure Key Vault
By utilising Azure Key Vault and the Microsoft.Extensions.Configuration.AzureKeyVault NuGet Package, you will be able to stash configurations for your projects in the best way possible in the actual environment.
You then simply inject it in,
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var root = config.Build();
config.AddAzureKeyVault(
$”https://{root["KeyVault:Vault"]}.vault.azure.net/",
root[“KeyVault:ClientId”],
root[“KeyVault:ClientSecret”]);
})
.UseStartup<Startup>();
Although you still have to stash those 3 variables in, Azure has Azure AD to enforce access only to specified Applications. Thus, you need to register an application under the Azure AD in order for this to work. There are also restrictive features that will help you sandbox Azure Key Vault further.
Existing Vault Storages
Last but not least, the last way is to utilise existing vault storage options like HashiCorp. Libraries like https://github.com/rajanadar/VaultSharp will help you to implement it quickly and effectively. This is suitable for you if you primarily use a non-Azure provider for your servers.
As described here, you can use appsettings.json, which is generated and referenced within the new project template in dotnet new command
You can use the ConfigurationBuilder from the Microsoft.Extensions.Configuration nuget package
Although the docs are for ASP Core, you should be able to use them in your regular .Net Core app.
Create settings.json:
{
"mysetting": "value",
}
And use it:
var configuration = new ConfigurationBuilder()
.AddJsonFile("settings.json")
.Build();
// get the values from the settings.json
var mySetting = configuration["mysetting"];
Console.WriteLine(mySetting);
I have an existing application. I am trying to port some pieces over from .NET 4.x over to .NET Core. I have created a context in my .NET Core app. I have create a db context via scaffold-dbcontext. I can run a basic query (hooray). Life is good. Now, I want to add in some async queries. I get the following error:
System.InvalidOperationException: 'The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.'
code:
var ctx = new GolfGameContext();
var userId = await (from u in ctx.AspNetUsers where u.Token == UserToken select u.Id).SingleAsync();
return userId;
This error seems strange. I have created some .NET Core 2.x apps from scratch and everything seems to work properly. I am able to do async queries with them just fine. When I look at the error link, I get taken to information about EF 6.x. I am guessing that there is something that the scaffold-dbcontext puts in the resulting models that cause this problem. I am also guessing that the code I have created in Core 2.x doesn't contain these same limitations. Am I on the right track? Do I need to change something in my context/models to get async to work properly? All thoughts are welcome.
TIA,
Wally
We have a legacy application built in Classic ASP that needs to access an API - this API is "protected" by an IdentityServer - so basically we need to implement OpenIdConnect support for this legacy application.
By far the easiest solution I can think of is trying to wrap this site somehow in a more updated version, then we could just add a few lines of code in the Owin-startup and everything should(?) be fine by adding:
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("api1");
options.Scope.Add("offline_access");
});
So it is possible to somehow get OWIN to work with Classic ASP? Or are we doomed to try and built our own client/middleware?
If there is no other "show-stopper" preventing you to update to .Net Framework 4.5.1+, than this is your right solution.
If you go to the Owin official NuGet page it will show you, that the library particularly has no dependencies, BUT this is not quite correct.
If we go to the source of the Microsoft.Owin we will see:
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
So it actually depends on .Net Framework 4.5.1. If you have the possibility to upgrade your project - do it.
Locally my project runs fine but when I deploy on Azure using a web app, I get the following error when it starts:
MissingMethodException: Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.Formatters.JsonOutputFormatter.get_SerializerSettings()'.
SmartAdmin.Startup.<>c.b__13_7(MvcOptions options)
I've tried this:
services.AddMvc(options =>
{
options.Filters.Add(new UserPreferencesLoaderAtrribute());
var jsonFormatter = (JsonOutputFormatter)options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter);
if (jsonFormatter != null)
{
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
});
And this:
services.AddMvc(options =>
{
options.Filters.Add(new UserPreferencesLoaderAtrribute());
}).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
Yes, I just worked all night and did eventually figured it out. Here is what you need to do:
Make sure you install:
-Microsoft.AspNet.Mvc.Formatters.Json version "6.0.0-rc1-final"
and
-Revert Netonsoft.Json to "6.0.6".
Then you can keep this:
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
project.json:
"Microsoft.AspNet.Mvc.Formatters.Json": "6.0.0-rc1-final",
"Newtonsoft.Json": "6.0.6"
I had a bunch of trouble redeploying too but eventually this worked.
Good luck!
Just got off a call with Microsoft support as of yesterday (02 Aug 2016) Azure App Services now only support ASP.NET core, due to a breaking change:
A breaking change was released and anything other than ASP.NET core is not supported, so the only option is an upgrade. The breaking change is being rolled out to all (regions) eventually all your instances will fail.
Is ASP.NET 5, Core RC1, RC2 supported on Azure App Service? NO
https://blogs.msdn.microsoft.com/waws/2016/06/14/supporting-asp-net-5-core-rc1-on-azure-app-service/
Verify your app is running the latest version of ASP.NET Core and not RC1 or RC2.
We were affected (North Europe) and upgraded our app from RC2 and it worked first time.
We also saw this in production, contacted the team and got this out: https://social.msdn.microsoft.com/Forums/en-US/f0a6bbaf-498a-4c1f-b869-6779ee18e04e/app-service-applications-may-experience-methodnotfound-exceptions-due-to-incorrect-newtonsoft-json?forum=windowsazurewebsitespreview
It appears that a fix for App Service is on its way as well. Meanwhile, the linked post contains pretty much the same instructions as the other answers here.