Could not load file or assembly Microsoft.IdentityModel.Tokens problem - c#

I am trying to verify users with a JWT token. The code I used below works perfectly fine in a console application. But when I want to apply it in my Azure function it gives me the error:
Could not load file or assembly Microsoft.IdentityModel.Tokens
I do have one other Azure function in my solution but it doesn't use this NuGet package. I already took a look at this link:
Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.0.0
I can't get anything out of that. So what am I doing wrong? Thanks in advance
string key = "";
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
var header = new JwtHeader(credentials);
var payload = new JwtPayload
{
{ "some ", "hello "},
{ "scope", "http://dummy.com/"},
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
var token = handler.ReadJwtToken(tokenString);
log.LogInformation(token.ToString());

Solved it by adding a line of code in the .csproj file
<PropertyGroup>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

I had this problem not when running in development, but on deployed projects. This did not relate to Azure project, but to a web application deployed onto a remote windows web server
I took these steps to resolve it:
Get the files names of the DLLs from references
Find these DLLS in the packages subfolder of your vs project.
Make sure you have the right version for your .net framework version
Copy the DLLs to a folder, we called it "deployments"
Remove the Nuget packages
Reference the DLLs directly with copylocal=true
You may or may not need to add the DLLs to the bin folder in your deployment package. The packages we needed are shown below

Related

NLog in different versions for multiple projects

In my project I am using NLog assembly with file version 4.6.6. My application is being loaded by the external app (no access to the source code) which also uses NLog with file version 4.7.2.
Everything from the external app and my app is being logged correctly. The strange thing that I am facing is that in my app I add the logging rules:
var uniqueLogName = "mylogger" + Guid.NewGuid();
var rule = new LoggingRule(uniqueLogName, ConvertLogLevel(SelectedLogLevel), myFileTarget);
LogConfig.LoggingRules.Add(rule);
var uniqueObjectsLogName = "myObjectslogger" + Guid.NewGuid();
var objectsRule = new LoggingRule(uniqueObjectsLogName, ConvertLogLevel(SelectedLogLevel), myObjectTarget);
LogConfig.LoggingRules.Add(objectsRule);
And for some reason I am getting folders with these rule names created on C drive (see the link below) and honestly, I don't even know where to look for the issue. Can this be some sort of configuration on the NLog?

Discover the version number of another package

The IoT Core application I'm working on needs to know the (package) version of another application.
Is it possible to find out the version of another app (without needing the admin password as in the example below)?
Unfortunately AppDiagnosticInfo doesn't provide this information (it provides package family name).
I know this is possible with Device Portal REST api, but this requires the device admin password which is not suitable for production scenarios.
//Import WindowsDevicePortalWrapper with NuGet
using Windows.System.Diagnostics.DevicePortal;
(...)
var devicePortalConnection1 = new DefaultDevicePortalConnection(
"http://127.0.0.1:8080",
"administrator",
"my device administrator password");
var portal = new DevicePortal(devicePortalConnection1);
var packages = await portal.GetInstalledAppPackagesAsync();
You can use PackageManager to get the installed packages version. It does not need username and password,but it runs on device locally.
Please refer to below code.
Windows.Management.Deployment.PackageManager packageManager = new Windows.Management.Deployment.PackageManager();
IEnumerable<Windows.ApplicationModel.Package> packages = (IEnumerable<Windows.ApplicationModel.Package>)packageManager.FindPackages();
foreach(var pkg in packages)
{
var version = pkg.Id.Version;
Debug.WriteLine(string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision));
}
To run above code, you also need to add a restricted capability to the Package.appxmanifest. Had to add a new namespace at the top of the file:
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
and add the following to the Capabilties tag.
<rescap:Capability Name="packageQuery" />
You may need to add the 'rescap' namespace to the Ignorable list as per this information.

MSBuild using BuildManager cannot find bootstrapper path or SignTool.exe

I'm trying to automate builds using the MSBuild API - "BuildManager".
The following code works fine for building solutions/projects but fails when it comes to publishing.
The project publishes just fine when using the Visual Studio Publish page.
var collection = new ProjectCollection();
var parameters = new BuildParameters(collection);
parameters.Loggers = new List<ILogger> { Logger };
parameters.MaxNodeCount = Environment.ProcessorCount; //maxcpucount
var globalProperties = new Dictionary<string, string>();
globalProperties.Add("Configuration", "Debug");
globalProperties.Add("Platform", "AnyCPU");
globalProperties.Add("OutDir", binPath);
globalProperties.Add("OutputPath", publishPath);
globalProperties.Add("ApplicationVersion", version.ToString());
globalProperties.Add("GenerateBootstrapperSdkPath", #"C:\Program Files (x86)\Microsoft SDKs\ClickOnce Bootstrapper\");
//Doesn't work!
globalProperties.Add("SignToolPath", #"C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe");
BuildManager.DefaultBuildManager.ResetCaches();
var buildRequest = new BuildRequestData(projectFile.FileInfo.FullName, globalProperties, "4.0", new[] { "Rebuild", "Publish" }, null);
var buildResult = BuildManager.DefaultBuildManager.Build(parameters, buildRequest);
If I run this code, it fails with the following error:
An error occurred while signing: SignTool.exe not found.
As you can see I'm adding the global property called "GenerateBootstrapperSdkPath", which if its not there leads to this error:
Could not find required file 'setup.bin' in
'C:\PathToProject\Engine'.
This seems to be a vicious cycle, as soon as I specify a path for the bootstrapper, it can't find the SignTool.exe, if I don't it can't find the setup.bin.
Unfortunately the global property "SignToolPath" doesn't seem to do anything.
Any Ideas?
There was an old problem of locating bootstrapper by MSBuild which could be solved by creating registry key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\GenericBootstrapper\4.0]
#="0"
"Path"="C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bootstrapper\\"
Just set it to your SDK version and try again. Or perhaps use the path to your bootstrapper in BuildManager code based on the above one instead of:
C:\Program Files (x86)\Microsoft SDKs\ClickOnce Bootstrapper\
According to one of old blog entries the following registry values are resolved in search for the bootstrapper path and finally your local project folder under the Engine sub folder is checked and when not found MSBuild throws MSB3147 error:
HKLM\Software\Microsoft\GenericBootstrapper\<.NET Tools Version>\
HKLM \Software\Microsoft.NetFramework\SDKInstallRoot\Bootstrapper
HKLM \Software\Microsoft\VisualStudio\\InstallDir\Bootstrapper
Team build for ClickOnce application with bootstrapper
I understand from your description that finding solution to bootstrapper location would solve your problem since in that configuration you did not observe any SignTool.exe problems.

Cybersource Simple Order Api. The system cannot find the file specified Error Message

I have a .Net website (not a Web Application) project running in framework version 4.6.2
My implementation of the Cybersource simple order api works correctly on the development machine. However when i deploy my website to stage or product i receive the error The system cannot find the file specified when attempting the following code below.
NVPClient.RunTransaction(_cyberSourceConfig, request);
I have checked that the keys are valid. The _cyberSourceConfig contains the correct Currency, MerchantId, KeyFile, Key Directory and the locations are valid within the file system.
Check that the following DLL's are included within the projects bin directory
I have also checked that the following DLL's are in my projects bin directory.
CyberSource.Base.dll
CyberSource.Clients.dll
CyberSource.Clients.xml
_cyberSourceConfig consists of the the following
private CyberSource.Clients.Configuration _cyberSourceConfig = new CyberSource.Clients.Configuration();
_cyberSourceConfig.ConnectionLimit = -1;
_cyberSourceConfig.KeyFilename = "setting for key file"
_cyberSourceConfig.KeysDirectory = "path to keys directory"
_cyberSourceConfig.MerchantID = "My merchant id"
_cyberSourceConfig.SendToProduction = false;
_cyberSourceConfig.ServerURL = "https://ics2wstesta.ic3.com/commerce/1.x/transactionProcessor/";
Based on your provided information, I would suspect that you are specifying a file that is resolvable on your development machine, but is not resolving properly on your staging / production server.
_cyberSourceConfig.KeyFilename = #"keys.foo";
_cyberSourceConfig.KeysDirectory = #"C:\KeysDirectory\MyKeys\"; // <-- are you sure?
You need to make sure that either the relative or absolute paths are the same or are resolving to the file properly when using different enviornments.
Set your App Pool to Load User Profile: True
http://www.daves-blog.net/post/2014/06/16/X509Certificate-The-System-cannot-find-the-file-specified.aspx
That link is going to a 404 now, but the more general answer is fleshed out in this question.
CryptographicException was unhandled: System cannot find the specified file

Skydrive the operation has timed out

I am trying to connect to skydrive with this code, it is console C# application:
var client = new SkyDriveServiceClient();
client.LogOn("username", "password");
and I got this exception: The operation has timed out.
Anyone know what is solution for this problem?
Ok, download the latest version (Changset 68942) from this
location. After download open the project (located in folder trunk/src/SkyDriveServiceClient.sln) in Visual Studio and change the variable SkyDocsServiceUri in the class SkyDocsServiceClient to the following:
public static readonly Uri SkyDocsServiceUri = new Uri("https://docs.live.net/SkyDocsService.svc");
After the change compile the project to build the assembly file. Then you need to reference the assembly from your project and try to execute your code again.

Categories

Resources