Test Aspnet.security.openidconnect.server with client application
Clash Royale CLAN TAG#URR8PPP
Test Aspnet.security.openidconnect.server with client application
I have created a project in .net core 2.1 and implemented ASOS, Now I want to test authorization flow like I want to open login screen and then I will select my ssl certificate and will get the token so for that purpose I have created a client app but it's returning 404 on /connect/authorization endpoint.
Here is the ASOS implementations:
services.AddAuthentication(OAuthValidationDefaults.AuthenticationScheme).AddOAuthValidation()
.AddOpenIdConnectServer(options =>
{
options.AuthorizationEndpointPath = new PathString(AuthorizePath);
// Enable the token endpoint.
options.TokenEndpointPath = new PathString(TokenPath);
options.ApplicationCanDisplayErrors = true;
options.AccessTokenLifetime = TimeSpan.FromMinutes(5);
#if DEBUG
options.AllowInsecureHttp = true;
#endif
options.Provider.OnValidateAuthorizationRequest = context =>
if (string.Equals(context.ClientId, Configuration["OpenIdServer:ClientId"], StringComparison.Ordinal))
context.Validate(context.RedirectUri);
return Task.CompletedTask;
;
// Implement OnValidateTokenRequest to support flows using the token endpoint.
options.Provider.OnValidateTokenRequest = context =>
// Reject token requests that don't use grant_type=password or grant_type=refresh_token.
if (!context.Request.IsClientCredentialsGrantType() && !context.Request.IsPasswordGrantType()
&& !context.Request.IsRefreshTokenGrantType())
context.Reject(
error: OpenIdConnectConstants.Errors.UnsupportedGrantType,
description: "Only grant_type=password and refresh_token " +
"requests are accepted by this server.");
return Task.CompletedTask;
if (string.IsNullOrEmpty(context.ClientId))
context.Skip();
return Task.CompletedTask;
if (string.Equals(context.ClientId, Configuration["OpenIdServer:ClientId"], StringComparison.Ordinal) &&
string.Equals(context.ClientSecret, Configuration["OpenIdServer:ClientSecret"], StringComparison.Ordinal))
context.Validate();
return Task.CompletedTask;
;
// Implement OnHandleTokenRequest to support token requests.
options.Provider.OnHandleTokenRequest = context =>
context.Request.IsPasswordGrantType())
//var identity = new ClaimsIdentity(context.Scheme.Name,
// OpenIdConnectConstants.Claims.Name,
// OpenIdConnectConstants.Claims.Role);
ClaimsIdentity identity = null;
if (context.Request.IsClientCredentialsGrantType())
identity = new ClaimsIdentity(new GenericIdentity(context.Request.ClientId, "Bearer"), context.Request.GetScopes().Select(x => new Claim("urn:oauth:scope", x)));
else if (context.Request.IsPasswordGrantType())
identity = new ClaimsIdentity(new GenericIdentity(context.Request.Username, "Bearer"), context.Request.GetScopes().Select(x => new Claim("urn:oauth:scope", x)));
// Add the mandatory subject/user identifier claim.
identity.AddClaim(OpenIdConnectConstants.Claims.Subject, Guid.NewGuid().ToString("n") + Guid.NewGuid().ToString("n"));
// By default, claims are not serialized in the access/identity tokens.
// Use the overload taking a "destinations" parameter to make sure
// your claims are correctly inserted in the appropriate tokens.
identity.AddClaim("urn:customclaim", "value",
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
var ticket = new Microsoft.AspNetCore.Authentication.AuthenticationTicket(
new ClaimsPrincipal(identity),
new Microsoft.AspNetCore.Authentication.AuthenticationProperties(),
context.Scheme.Name);
// Call SetScopes with the list of scopes you want to grant
// (specify offline_access to issue a refresh token).
ticket.SetScopes(
OpenIdConnectConstants.Scopes.Profile,
OpenIdConnectConstants.Scopes.OfflineAccess);
context.Validate(ticket);
return Task.CompletedTask;
;
and here is the client app:
public void ConfigureServices(IServiceCollection services)
services.AddMvc();
services.AddAuthentication(options =>
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "GitHub";
)
.AddCookie()
.AddOAuth("GitHub", options =>
options.ClientId = "4trgdff3dfadffsfsa";
options.ClientSecret = "sds4546ddsdsdsdsds";
//not sure about the callback
options.CallbackPath = new PathString("/login");
options.AuthorizationEndpoint = "https://localhost/WebApp/connect/authorize";
options.TokenEndpoint = "https://localhost/WebApp/connect/token";
options.UserInformationEndpoint = "https://localhost/WebApp/userinfo";
options.SaveTokens = true;
//options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
//options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
//options.ClaimActions.MapJsonKey("urn:github:login", "login");
//options.ClaimActions.MapJsonKey("urn:github:url", "html_url");
//options.ClaimActions.MapJsonKey("urn:github:avatar", "avatar_url");
options.Events = new OAuthEvents
OnCreatingTicket = async context =>
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
context.RunClaimActions(user);
;
);
Here is the controller method:
[HttpGet]
public IActionResult Login(string returnUrl = "/")
return Challenge(new AuthenticationProperties() RedirectUri = returnUrl );
Here is the error:
This localhost page can’t be found
No webpage was found for the web address: https://localhost/WebApp/connect/authorize?client_id=TWXO02aIDkK7564eoAh0wQ&scope=&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A54108%2Flogin&state=CfDJ8LygGi9uYeFEkbvAts2SZpy6ytnFjrcNCCmeVAA15dDCN7fSq4IEX2A6OUeEWOmpxrFovAciUEwUIaV9WKer23ty_RGMWQ0YewdllgE2bicTfzHcl-d9RvHGuIKmknE3_lF_o0GETgCrPcjkMvarC8CpSOQGKl88FsZg3Zpk1YXmhvS_vHBkU_hKipl27ivYzVHK1VVKIWYLmR4FSrtu7Ic
I found this client app implementation from this link https://www.jerriepelser.com/blog/authenticate-oauth-aspnet-core-2/ Now I am not sure what is wrong, Also my app with ASOS implementation is deployed on local iis
Any help?
I hsd also tried that, but got same results. Second thing is my app is deployed on local iis.
– Ask
Aug 7 at 3:55
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Perhaps you should specify the port number.
– Ruard van Elburg
Aug 6 at 19:38