How to use RSA in .net Core - asp.net-core

I try to encrypt a file with RSA, but it don't have the toXmlString() and fromXmlString method. how to use RSA class in .net core ?
And I want to encrypt with private key and decrypt with public key ,so others can only read this file but can't generate this file , Is that possible ?

While the ToXmlString and FromXmlString methods are not available, ImportParameters(RSAParameters) and ExportParameters(bool) are.
If you have existing XML formatted values that you need to continue to read, the XML format of the keys isn't very interesting:
Public only:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
</RSAKeyValue>
Public+Private:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
<P>[base64-encoded value]</P>
<Q>[base64-encoded value]</Q>
<DP>[base64-encoded value]</DP>
<DQ>[base64-encoded value]</DQ>
<InverseQ>[base64-encoded value]</InverseQ>
</RSAKeyValue>
(where each named XML element maps to the field of the same name on the RSAParameters struct)
Otherwise you can/should use whatever serialization mechanism is the best suited for you and your application.

Currently you get less when using pure .net core:
In the end this means you can build on .NET Core today, and expect
more features to light up later, so things will get easier as time
goes on. Whether you want to be an early adopter with the less
featured framework now, or jump in later when more features have been
added and the third party eco-system has caught up is going to be a
(tough) decision that we all have to make on your own.
You will have them available if you target the full .net framework in .net core.
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"net461": {}
}
}
They won't be available with for example:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

Related

OData: Change URL value for entity type in EDM Model

I'm working with a ASP.NET Core Web Api project that uses OData for the exposed endpoints and are consumed with a Simple.OData.Client.
Some of my endpoints are:
http://{baseUrl}/odata/Vehicle --> this works perfectly
But I'm having issues with these two:
http://{baseUrl}/odata/Vehicle/Brand
http://{baseUrl}/odata/Vehicle/Type
Basicly, I can't modify my EDM Models for modifying the URL property that is exposed in the metadata of OData. My EDM looks like this:
private IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<Vehicle>("Vehicle");
odataBuilder.EntitySet<VehicleType>("VehicleType");
odataBuilder.EntitySet<VehicleBrand>("VehicleBrand");
return odataBuilder.GetEdmModel();
}
And the metadata that I get when I navigate through http://{baseUrl}/odata/ is the following:
{
"#odata.context": "https://localhost:44332/odata/$metadata",
"value": [
{
"name": "Vehicle",
"kind": "EntitySet",
"url": "Vehicle"
},
{
"name": "VehicleType",
"kind": "EntitySet",
"url": "VehicleType"
},
{
"name": "VehicleBrand",
"kind": "EntitySet",
"url": "VehicleBrand"
}
]
}
I couldn't find a way to maintain the name as it is, but modify the "url" property shown on the JSON to point to my proper endpoint. I want this result:
{
"name": "VehicleBrand",
"kind": "EntitySet",
"url": "Vehicle/Brand"
}
Any of the methods exposed on EntitySetConfiguration or ODataConventionModelBuilder seems to have a way to specify a different URI for a registered entity type.
Someone has faced this issue? I'm sure that might be some way of solving this.
Odata Route or Navigation Property?
Please have a look at that documentation here
Long story short - an OData URI consists of:
The service root
The OData path
Query options
For example. This is a Path that goes to the EntitySet "Products", takes the first, and then Navigates (see Navigation Properties) to its Supplier.
https://example.com/odata/Products(1)/Supplier?$top=2
------------base---------|-----Path-----------?---options---
So, everything you make accessible at root level should have its own path, and the / telling Odata to navigate onward from there.
So, now for OData, it would freak the hell out of most clients and surely be bad style if you would define an entitysets path as something that can be confused with another entititysets navigation property.
But if you REALLY need to do it, maybe you can achieve it by defining a custom routing convention.
But dont! It will only make trouble
Do you want a navigation property?
If you want the set that "Type" returns to be dependent on the Vehicle, you should define a navigation property on Vehicle instead.
Greetings, Mike

Should "persistent" field appear in serialized PanacheEntity?

I am building a simple Jaxrs api in quarkus. When I call my index method it looks like jackson serializes the objects with the persistent field, that it gets from the PanacheEntityBase.
Example:
[
{
"persistent": true,
"id": 1,
"createdAt": "2019-03-18",
"updatedAt": "2019-03-18"
},
{
"persistent": true,
"id": 2,
"createdAt": "2019-03-18",
"updatedAt": "2019-03-18"
}
]
The persistent field isn't saved to the database, but it shows up in the response. I have looked into using #jsonIgnore and jackson mixins, but I would rather not have to do this, especially if this is just a configuration issue. I am curious if Panache should be doing this, or if anyone else is having this problem.
This happens when we use 3-rd party libraries as returned data type and provide it to Jackson serialisation process. PanacheEntity extends PanacheEntityBase which contains isPersistent method which is treated by Jackson like a POJO getter method.
public boolean isPersistent() {
return JpaOperations.isPersistent(this);
}
Jackson automatically takes all get* and is* methods and try to serialise it and include to result JSON. There is no way to configure it on quarkus level. Your solution with JsonIgnore and MixIn feature is good approach.
With Json-B add in your entity :
#JsonbTransient
public boolean isPersistent() {
return super.isPersistent();
}

Strongly typed configuration asp core

I am trying to create a strongly typed config section but struggling. Examples show that I can have a POCO and simply have an entry in my json this should automatically resolve.
This is what I have in ConfigureServices(). Please note, the configuration is IConfigurationRoot:
public void ConfigureServices(IServiceCollection services)
{
services
.AddOptions()
.AddMvcCore()
.AddJsonFormatters();
services.Configure<MySettings>(this.configuration.GetSection("MySettings"));
}
This is my POCO
public class MySettings
{
public string Foo { get; set; }
}
I get a compiler error Error:(41, 44) : Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action<MySettings>'.
The JSON config:
{
"MySettings": {
"Foo": "hello world"
}
}
Clearly, I am doing something silly but unsure what this could be. All sources on the web suggest this "should" work.
If further info is required then I can provide that.
You are missing
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
in your project.json file.
(The version may be different in your specific case)
A more complete answer is that you need to add the following nuget package to your ASP Core Project if you want to configure the strongly typed config in that way.
Microsoft.Extensions.Options.ConfigurationExtensions
The extension methods contained in the package will allow you to configure the strongly typed configuration the way you want to and the way most tutorials show.
services.Configure<MyOptions>(Configuration);
Alternatively, you could add another binder package:
Microsoft.Extensions.Configuration.Binder
Configuration would then look something like this:
services.AddOptions();
services.Configure<MyOptions>(x => Configuration.Bind(x));
This is the downside of having so many modular packaged up extensions. It gets easy to lose track of where functionality exists.

Using resx resources in .NET Core 1.0

I am attempting to use a resource(.resx) file in an ASP.NET Core project. I had it working in rc1 using a namedResource, but can't seem to get it working in RTM. Here is what my project.json looks like:
"buildOptions": {
"embed": {
"include": [ "Resources/resource.resx" ]
}
}
And I'm accessing it like this:
Resources.ResourceManager.GetString("Field1");
It seems the resources are not getting embedded as There is nothing there when I debug.
Any help would be much appreciated.
For string in resource use:
Remove embed.
var x = ResourceFoo.Field1;
For file in resource use:
Include embed for file (ex.: /Folder1/**/*)
Assembly.GetExecutingAssembly().GetManifestResourceStream("Folder1.Folder2.file.ext");

EF 7 alpha 3: Azure Table Storage

I'm trying to get an example of EF 7 with Azure Table Storage to work in VS 14 CTP3, but I am having no luck with the dependency injection stuff. I was able to get an example with SQL done fairly easily, but I am seeing an issue that doesn't make sense: The referenced package is there and being pulled in, and if I look at it, it contains the correct namespaces, methods, clases etc., but the compile doesn't like it.
Here is my project.json:
{
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-alpha3",
"EntityFramework.AzureTableStorage": "7.0.0-alpha3",
"Microsoft.AspNet.RequestContainer": "1.0.0-alpha3"
},
"frameworks" : {
"net451" : { },
"k10" : { }
}
}
using System;
using Microsoft.AspNet.Builder;
using Microsoft.Data.Entity; /* <- 'missing reference' unless I add EntityFramework to project.json */
using Microsoft.Data.Entity.AzureTableStorage; /* <- ALWAYS errors w/ 'missing reference' */
using Microsoft.Framework.DependencyInjection;
namespace WebApplication2
{
public class Startup
{
public void Configure(IBuilder app)
{
app.UseServices(services =>
{
services.AddEntityFramework() /* <-- this errors too */
.AddAzureTableStorage();
services.SetupOptions<DbContextOptions> //,- says it can't find this
(config => config.UseAzureTableStorage("UseDevelopmentStorage=true"));
});
}
}
}
The strange thing is, if I right click and 'go to definition' on any of the 'missing' classes or methods, it brings them up, and I can see that I'm using them as defined. Am I missing something terribly obvious? Or is this stuff just not fully cooked yet?
Your project.json has both frameworks mentioned so VS builds both of them. If your intention is to just build for net451, you should remove the following from your project.json -
"k10" : { }