question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple Password Grant fails after alpha2-0410

See original GitHub issue

I am able to get a token via password grant flow on version 1.0.0-alpha2-0410 and older, but in any newer versions (NuGet skips forward to 0417) password grant won’t give me a token, it 404s.

The only clue I have is that when it was working it complained : AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware: Information: No explicit audience was associated with the access token.

With newer versions it doesn’t complain about audience but doesn’t give me a token either, just 404s. It complains normally about missing fields if I break the request.

This most of my code, it is very simple with nothing custom added in the database (local MS-SQL).

ConfigureServices

            services.AddMvc();

            // Add the database context, defaults to scoped; new context for each request.
            services.AddDbContext<QbDbContext>(
                options => { options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")); });

            // Adds Identity to IoC and confugres with the database.
            services.AddIdentity<QbUser, IdentityRole>(ConfigureIdentityOptions)
                .AddEntityFrameworkStores<QbDbContext>()
                .AddDefaultTokenProviders();

            services.AddOpenIddict<QbUser, QbDbContext>()
                .EnableTokenEndpoint("/api/auth/token") // Password grant route
                .AllowPasswordFlow() // Enables password grant
                .DisableHttpsRequirement() // TODO: Must unset this in production
                .AddEphemeralSigningKey() // TODO: Must unset this in production
                .SetAccessTokenLifetime(TimeSpan.FromDays(365));
        private static void ConfigureIdentityOptions(IdentityOptions options)
        {
            options.SignIn.RequireConfirmedEmail = false;
            options.SignIn.RequireConfirmedPhoneNumber = true;
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequiredLength = 8;
        }

Configure


            app.UseIdentity(); // Authorization using ASP Identity.
            app.UseOAuthValidation();
            app.UseOpenIddict(); // OpenIddict takes care of the token issuing.

            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc();
    public class QbUser : OpenIddictUser
    {
    }
    public class QbDbContext : OpenIddictDbContext<QbUser>
    {
        public QbDbContext(DbContextOptions options) : base(options)
        {
        }
    }

Am I doing something obvious wrong or is there a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kevinchaletcommented, Aug 17, 2016

It’s definitely an unwanted consequence of the recent design change I mentioned. Looks like we’ll have to rework it a bit 😄

0reactions
kevinchaletcommented, Aug 25, 2016

Will be fixed by https://github.com/openiddict/openiddict-core/pull/203. The approach I have in mind is to make having your own “token endpoint action” mandatory to support password token requests. i.e you’ll have to include something like that in your project:

[HttpPost("~/connect/token")]
public async Task<IActionResult> Exchange() {
    var request = HttpContext.GetOpenIdConnectRequest();

    if (request.IsPasswordGrantType()) {
        var user = await _userManager.FindByNameAsync(request.Username);
        if (user == null) {
            return Json(new OpenIdConnectResponse {
                Error = OpenIdConnectConstants.Errors.InvalidGrant
            });
        }

        // Ensure the password is valid.
        if (!await _userManager.CheckPasswordAsync(user, request.Password)) {
            if (_userManager.SupportsUserLockout) {
                await _userManager.AccessFailedAsync(user);
            }

            return Json(new OpenIdConnectResponse {
                Error = OpenIdConnectConstants.Errors.InvalidGrant
            });
        }

        if (_userManager.SupportsUserLockout) {
            await _userManager.ResetAccessFailedCountAsync(user);
        }

        var identity = await _userManager.CreateIdentityAsync(user, request.GetScopes());

        // Create a new authentication ticket holding the user identity.
        var ticket = new AuthenticationTicket(
            new ClaimsPrincipal(identity),
            new AuthenticationProperties(),
            OpenIdConnectServerDefaults.AuthenticationScheme);

        ticket.SetResources(request.GetResources());
        ticket.SetScopes(request.GetScopes());

        return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
    }

    return Json(new OpenIdConnectResponse {
        Error = OpenIdConnectConstants.Errors.UnsupportedGrantType
    });
}

(of course, token requests will still be validated by OpenIddict itself before MVC is invoked)

Read more comments on GitHub >

github_iconTop Results From Across the Web

what's the alternative to password grant now that it is ...
The Resource Owner Password Grant solved your problem perfectly fine, but now it got deprecated :( Note It says in the OAuth 2.0...
Read more >
Don't use the OAuth password grant type - Scott Brady
The resource owner password credentials grant type involves the application impersonating the user. When you use OAuth, you're after delegation ...
Read more >
What is the OAuth 2.0 Password Grant Type?
The Password grant is one of the simplest OAuth grants and involves only one step: the application presents a traditional username and password ......
Read more >
Implementing the password grant type | Apigee Edge
The following flow diagram illustrates the resource owner password grant type flow with Apigee Edge serving as the authorization server. Tip: To see...
Read more >
OAuth 2.0 Password Grant Type
The latest OAuth 2.0 Security Best Current Practice disallows the password grant entirely, and the grant is not defined in OAuth 2.1. More...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found