I'm working on a project which has angular as Frontend and backend API in asp.net WebAPI. There is an OpenId connect server(mitreid) which is hosted and I have to use that server for authentication and authorization.
I am trying to configure OpenId Connect server authentication to .net WebAPI, this is what I've done in StartUp.cs of WebAPI
app.SetDefaultSignInAsAuthenticationType("OpenIdConnect");
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = "some_client",
ClientSecret = "some_secret",
Authority = "http://localhost:8181/openid-connect-server-webapp",
UseTokenLifetime = true,
ResponseType = "code",
Scope = "openid email",
SignInAsAuthenticationType = "OpenIdConnect",
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
return Task.FromResult(0);
},
RedirectToIdentityProvider = async n =>
{
n.ProtocolMessage.RedirectUri = "http://localhost:54464/";
n.OwinContext.Authentication.Challenge(new String[] { "OpenIdConnect" });
},
SecurityTokenValidated = (n) =>
{
var nid = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType, "user", "user");
nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString()));
nid.AddClaim(new Claim("app_specific", "some data"));
n.AuthenticationTicket = new AuthenticationTicket(nid, n.AuthenticationTicket.Properties);
return Task.FromResult(0);
},
SecurityTokenReceived = (context) =>
{
var code = context;
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
//some redirect
context.HandleResponse();
return Task.FromResult(0);
}
}
});
I tried setting configuration even, like below
ConfigurationManager =
new ConfigurationManager<OpenIdConnectConfiguration>
("http://localhost:8181/openid-connect-server-webapp/.well-known/openid-configuration"),
Configuration = new OpenIdConnectConfiguration
{
Issuer = "http://localhost:8181/openid-connect-server-webapp",
AuthorizationEndpoint = "http://localhost:8181/openid-connect-server-webapp/authorize",
TokenEndpoint = "http://localhost:8181/openid-connect-server-webapp/token",
UserInfoEndpoint = "http://localhost:8181/openid-connect-server-webapp/userinfo"
},
When I try to debug the API (at port 54464), I put authorize attr on a controller, it is redirecting to the OpenID login page and then after successful login and then no controller action is being executed.
let's say this is the API call I'm testing
http://localhost:54464/api/common/getlist
initially for first call API redirects to the login page and then redirecting to http://localhost:54464/api/common/getlist/?code=V7KFPZ&state=OpenIdConnect.AuthenticationProperties%3D****somecode****
instead of returning a JSON array.
I tried generating Bearer token using code from above-redirected URL from postman which was successful. however, when tried to use the token in the authorization header using postman, the response is OpenID login HTML page.
The issue none of the notification events are executing other than RedirectToIdentityProvider
I know I'm missing something, please point me those, I'm fairly new to this. let me know if I'm doing something wrong, some configuration mistakes, or any solutions implementing OpenId client.