Loading class IWebUI from ADAL library fails - azure-active-directory

We get this error at runtime in our C# application:
{"Could not load type 'Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.IWebUI' from assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.13.1.846, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.":"Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.IWebUI"}
thanks

It seems you were missing the assembly ADAL required. If you were developing with Visual Studio, I suggest that you use the NuGet to manage the libraries.
For example, I created a console application which use ADAL to acquire the token from Azure AD. Then we can install the ADAL by right click the reference->Manage NuGet Packages and search/install the ADAL like figure below:
And below is the code I acquired the token successfully:
static void Main(string[] args)
{
AccquireToken();
Console.ReadLine();
}
static async void AccquireToken()
{
string authority = "https://login.microsoftonline.com/common";
string resource = "https://graph.microsoft.com";
string clientId = "ce1c938c-001c-4caf-b078-9092103e1d49";
Uri redirectUri = new Uri("http://localhost");
AuthenticationContext authContext = new AuthenticationContext(authority);
var result = await authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
Console.WriteLine(result.AccessToken);
}
And in this case, the type of Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.IWebUI is in Microsoft.IdentityModel.Clients.ActiveDirectory.Platform assembly which will be installed/referenced automatically when we used the NuGet to install the ADAL library.

I had to delete this file:
Not sure where it came from. It was not referenced directly. Must have been a relic from a previous version or reference or something. But deleting it worked.

Related

xUnit test project connection string

I would like to know the recommended approach to getting a connection string from a config file for my xUnit .net core test project.
I have set up a test project using the new Visual Studio 2017 xUnit Test project template for .net core. This project will run my integration tests that reference 2 different .net core class library projects - one of which will talk to the database using EF Core.
I understand that normally the connection string should not be set or accessed in a class library project - it should be the application that consumes the class library that should set the connection string.
However, in this case it appears that the xUnit test project is treated somewhat like a class library project. I have not seen any examples of how to set up some sort of config file and access that from the test project. How do I access the connection string from a config file so that my test project can consume my Datalayer class library project and pass in the appropriate connection string?
I was able to access the connection string from my xUnit test project by creating a DbOptionsFactory class that returns a DbContextOptions object initialized with a connection string that it reads from an appsettings.json configuration file.
This requires a dependency on Microsoft.Extensions.Configuration
public static class DbOptionsFactory
{
static DbOptionsFactory()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var connectionString = config["Data:DefaultConnection:ConnectionString"];
DbContextOptions = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlServer(connectionString)
.Options;
}
public static DbContextOptions<MyDbContext> DbContextOptions { get; }
}
appsettings.json
{
"Data": {
"DefaultConnection": {
"Name": "MyDbContext",
"ConnectionString": "connection string goes here"
}
}
}
When instantiating my DbContext I pass in the optionsBuilder object that has the connection string from the configuration file like so:
using (var context = new MyDbContext(DbOptionsFactory.DbContextOptions))
{
// access db here
}
Hope this helps anyone else that runs into the same issue.

Spring-boot: add application to tomcat server

I have a back-end which is build on spring-boot and then some custom code from my school built upon that.
The front-end is pure angular application which I serve from a different server trough a gulp serve.
They're only connected by REST calls.
There's already an authentication module running on the backend and to now I need to serve this angular application from the same tomcat server the back-end is running on so it can also use this authentication module.
I've found this about multiple connectors so I copied it as following class to set up multiple connectors:
#ConfigurationProperties
public class TomcatConfiguration {
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
//tomcat.addAdditionalTomcatConnectors(createSslConnector());
return tomcat;
}
private Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
File keystore = new ClassPathResource("keystore").getFile();
File truststore = new ClassPathResource("keystore").getFile();
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(8443);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(keystore.getAbsolutePath());
protocol.setKeystorePass("changeit");
protocol.setTruststoreFile(truststore.getAbsolutePath());
protocol.setTruststorePass("changeit");
protocol.setKeyAlias("apitester");
return connector;
} catch (IOException ex) {
throw new IllegalStateException("can't access keystore: [" + "keystore"
+ "] or truststore: [" + "keystore" + "]", ex);
}
}
}
Problem is that I don't see or find how I should setup these connectors so they serve from my angularJS build folder.
Upon searching I came upon Spring-Boot : How can I add tomcat connectors to bind to controller but I'm not sure if in that solution I should change my current application or make a parent application for both applications.
My current application main looks like this:
#Configuration
#ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
#EnableAutoConfiguration
#EnableSpringDataWebSupport
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
If possible I'd like some more info about what connectors are in the spring-boot context.
If this is not the way to go I'd like someone to be able to conform this second solution or suggest a change in my code.
I'm really not sure enough about these solution that I want to go breaking my application over it. (though it's backed up with github)
Just place your AngularJS + other front-end assets into src/main/resources/static folder, Spring Boot will serve them automatically.

Cloud Endpoints API does not authorise Google APIs Explorer after upgrading to App Engine 1.9.0

I use the Google APIs Explorer to test my Cloud Endpoint API running on the local dev server. I have authentication turned on:
#Api(
name = "testapi",
version = "1.0",
description = "Test API",
clientIds = { ApiConstants.WEB_CLIENT_ID, ApiConstants.ANDROID_CLIENT_ID,
ApiConstants.IOS_CLIENT_ID, ApiConstants.EXPLORER_ID },
audiences = { ApiConstants.ANDROID_AUDIENCE })
public class TestAPI
{
#ApiMethod(name = "TextObject.get", path = "testobject/{id}")
public TestObject get(#Named("id") Long id, User user) throws OAuthRequestException
{
if (user == null)
{
throw new OAuthRequestException(
"No User specified when calling protected API method.");
}
...
where ApiConstants.EXPLORER_ID is set to:
public static final String EXPLORER_ID = com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID;
Everything worked fine in 1.8.9, and it works fine again when downgrading to 1.8.9. In 1.9.0, I get a null user in the code, and the "This method requires you to be authenticated." message in the explorer, even after logging in on the slider.
For now, I'll just hold off upgrading to 1.9.0, but I do want to upgrade at some stage to try out the Modules API.
Any ideas? Many thanks in advance.

Access Denied exception when using google-api-java-client

I am trying to use google-api-java-client for OAuth2.0 to create a simple 3rd party app to access an OAuth2.0 based webservices.
The programs breaks when I try to initialize
private static final HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
They are imported as:
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
It is a simple Web Application Project using Google App Engine plugin inside Eclipse.
Caused by: java.security.AccessControlException: access denied (java.net.NetPermission getProxySelector)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:376)
at java.security.AccessController.checkPermission(AccessController.java:549)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:383)
at java.net.ProxySelector.getDefault(ProxySelector.java:73)
at com.google.api.client.http.apache.ApacheHttpTransport.newDefaultHttpClient(ApacheHttpTransport.java:157)
at com.google.api.client.http.apache.ApacheHttpTransport.(ApacheHttpTransport.java:100)
at com.mytest.demo.TestApiDemoServlet.(TestApiDemoServlet.java:17)
I am using App Engine 1.8.4 and google-api-java-client 1.16.0-rc
Any help would be greatly appreciated!
You can't use ApacheHttpTransport in the GAE environment as this is retricted. This is the reason it is failing. You need to use UrlFetchTransport instead and it should work. To use this you need to import the corresponding jars from this link directly and drop in your libs or jar folder-
https://code.google.com/p/google-http-java-client/wiki/Setup#google-http-client-appengine

Hosting nancy with asp.net on IIS 6 - failed - wrong configuration

I tried some stuff to host a little nancy test api under IIS 6:
https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-asp.net
http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
But it dont work. Here are my steps:
Create Empty Nancy Web Application
Add Reference with nuget - Nancy.Hosting.Aspnet Version 0.15.1
new Web.config is modifyed automatically
as described in the wiki
Add new class in solution root - HelloModule.cs
insert test code "HelloWorld"
Publish the web site local
on Windows 2003
with a virtual Directory in the IIS manager
Browsing the url 'localhost/nancyTest' brings an HTTP 403 ERROR.
A little ASP.NET WebApplication runs with the same configuration.
The nancyTest application does not have a start site like default.aspx. I want to get the request response from .../nancyTest/ coded as:
public class HelloModule : NancyModule
{
public HelloModule()
{
Get["/"] = parameters => "Hello World";
}
}
Perhaps the call .../nancyTest/ is not a GET Request? Are there other things to go in more detail?
I know not so many people user IIS6 nowadays, but there is the following solution, i wish it can help some people that still use this old one,
Config aspnet_isapi to handle a new ext files and like , .start
Set default page for this application is index.start
In nancy module add the redirect method, like the follwing:
Get["index.start"] = _ => {
return Response.AsRedirect("~/", Nancy.Responses.RedirectResponse.RedirectType.Permanent);
};
wish it helps

Resources