sadlermd
asked on
OWIN OpenID MVC5 - Dynamically Change the OpenID Service Provider URL...
Tools: VS2013, C#, MVC5
I have defined an OpenID provider in Startup.Auth.cs.
When you click on the OpenID button on the login page, you are directed to the ExternalLogin action in the AccountController. So far, so good.
My question is this:
How\when can I dynamically change the URL for the OpenID service provider that was set in Startup.Auth.cs?
Thanks in advance,
Rick
I have defined an OpenID provider in Startup.Auth.cs.
When you click on the OpenID button on the login page, you are directed to the ExternalLogin action in the AccountController. So far, so good.
My question is this:
How\when can I dynamically change the URL for the OpenID service provider that was set in Startup.Auth.cs?
Thanks in advance,
Rick
Can you show me the code in Startup.Auth.cs for the provider please?
ASKER
I am using this library:
https://github.com/owin-middleware/OwinOAuthProviders
one of the (many) sample implementations is this:
app.UseOpenIDAuthenticatio n("https://www.google.com/accounts/o8/id", "Google");
https://github.com/owin-middleware/OwinOAuthProviders
one of the (many) sample implementations is this:
app.UseOpenIDAuthenticatio
If you are asking for a way to change the URL for OpenID authentication, it seems like it is right in front of me:
app.UseOpenIDAuthenticatio n("https://www.google.com/accounts/o8/id", "Google");
app.UseOpenIDAuthenticatio
ASKER
That is set during Startup;
What i need to do is change that URL after they click login and are redirected to the ExternalLogin() action.
I would like to change that provider URL dynamically in ExternalLogin().
Part of the problem is having a valid instance of IAppBuilder.app in the controller.
What i need to do is change that URL after they click login and are redirected to the ExternalLogin() action.
I would like to change that provider URL dynamically in ExternalLogin().
Part of the problem is having a valid instance of IAppBuilder.app in the controller.
Now that I understand your question, I would think that you could solve this problem by keeping an instance of the OAuth provider, but I am not sure if there is a property for the URL.
ASKER
This command returns the list of providers:
"HttpContext.GetOwinContex t().Authen tication.G etExternal Authentica tionTypes( )"
However, it only provides the Caption (i.e. "Google") and the AuthenticationType ("ExternalCookie").
If u have a code example of how u might proceed with keeping an instance of the provider around, that would be a big help!
thanks
"HttpContext.GetOwinContex
However, it only provides the Caption (i.e. "Google") and the AuthenticationType ("ExternalCookie").
If u have a code example of how u might proceed with keeping an instance of the provider around, that would be a big help!
thanks
I downloaded the provider source code, and when I use that code, I get this exception:
'Microsoft.Owin.Security.I Authentica tionManage r' does not contain a definition for 'GetExternalAuthentication Types' and no extension method 'GetExternalAuthentication Types' accepting a first argument of type 'Microsoft.Owin.Security.I Authentica tionManage r' could be found (are you missing a using directive or an assembly reference?
The version for Microsoft.Owin.Security is 2.1.0.0.
'Microsoft.Owin.Security.I
The version for Microsoft.Owin.Security is 2.1.0.0.
I did find GetAuthenticationTypes, which returns IEnumerable<Authentication Descriptio n>. That only provides a description, not an instance. I am still looking for the magic for getting the instance of the provider.
ASKER
I'm glad I'm not the only one who feels like magic is required!
Looking forward to hearing your findings...
Looking forward to hearing your findings...
I was able to get the OpenIDAuthenticationOption s instance, which has the discovery URI for Google. When the AppBuilder.Use is called, it stores some information about the middleware element in the _middleware field:
This reflection code, sets up Google OAuth, which stores the URI in the OpenIDAuthenticationOption s, and then pulls the options back from the _middleware field. I haven't found a property to access the options.
Another possibility, though, might be to create your own class, which inherits from OpenIDAuthenticationMiddle ware, and then extend the class to give you the functionality that you need.
public IAppBuilder Use(object middleware, params object[] args)
{
this._middleware.Add(AppBuilder.ToMiddlewareFactory(middleware, args));
return this;
}
This reflection code, sets up Google OAuth, which stores the URI in the OpenIDAuthenticationOption
Another possibility, though, might be to create your own class, which inherits from OpenIDAuthenticationMiddle
app.UseOpenIDAuthentication("https://www.google.com/accounts/o8/id", "Google");
var field = app.GetType().GetField("_middleware", BindingFlags.NonPublic | BindingFlags.Instance);
var middlewareList = (IList<Tuple<Type, Delegate, object[]>>)field.GetValue(app);
var optionList = middlewareList
.SelectMany(x => x.Item3)
.ToList();
var openIdOptions = optionList
.OfType<OpenIDAuthenticationOptions>()
.FirstOrDefault();
if (openIdOptions != null)
{
var uri = openIdOptions.ProviderDiscoveryUri;
Console.WriteLine(uri);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Using multiple OpenIDAuthenticationMiddle ware instances is probably the better approach, but I want to explore the other possibility of getting an instance of the class, too.
ASKER
This approach provided a simple but dynamic solution to the problem.
1. Create table that contains details required when calling UseOpenIDAuthentication().
2. Add method that retrieved table content.
3. Iterate over list and call UseOpenIDAuthentication(ke y, value);
Bob's answer could possibly work, however I didn't get a chance to test it.
1. Create table that contains details required when calling UseOpenIDAuthentication().
2. Add method that retrieved table content.
3. Iterate over list and call UseOpenIDAuthentication(ke
Bob's answer could possibly work, however I didn't get a chance to test it.