You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure B2C技术问题:OnAuthorizationCodeReceived事件无法触发

Hey Mike, let's break down this OpenID Connect authentication issue you're stuck on—since you mentioned existing resources didn't help, let's focus on the gaps in the code snippet you shared.

Key Missing Configurations in Your OpenID Connect Setup

Looking at your ConfigureAuth method, the OpenIdConnectAuthenticationOptions block is incomplete (the P... tells me you cut off critical parts). These are the non-negotiable settings you need to get this working:

  • Authority: This points to your identity provider's (IdP) base URL—whether it's Azure AD, Okta, a custom Auth0 tenant, etc. Without this, your app doesn't know where to send authentication requests. Example:
    Authority = "https://login.microsoftonline.com/your-tenant-id/v2.0",
    
  • ResponseType: Defines the type of authentication response you want. For production, the authorization code flow (with ID token) is the safest choice. Example:
    ResponseType = OpenIdConnectResponseType.CodeIdToken,
    
  • Scope: Specifies the permissions/user data you're requesting. At minimum, you need openid (required for OpenID Connect) and profile (basic user info). Add email if you need user email addresses:
    Scope = "openid profile email",
    
  • Notifications: This handles critical callback events during authentication—like exchanging the authorization code for an access token, or handling failures. A basic implementation looks like this:
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
        AuthorizationCodeReceived = async context =>
        {
            // Exchange authorization code for access token
            var tokenClient = new TokenClient(
                $"{context.Options.Authority}/connect/token",
                clientId,
                clientSecret); // Make sure clientSecret is pulled from web.config too
            
            var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(
                context.Code, context.Options.RedirectUri);
            
            // Store the access token in the user's claims for later use
            context.AuthenticationTicket.Identity.AddClaim(
                new Claim("access_token", tokenResponse.AccessToken));
        },
        AuthenticationFailed = context =>
        {
            // Handle authentication errors (e.g., log, redirect to error page)
            context.HandleResponse();
            context.Response.Redirect($"/Error?error={Uri.EscapeDataString(context.Exception.Message)}");
            return Task.CompletedTask;
        }
    }
    

Quick Checks to Rule Out Simple Issues

  • Double-check that clientId, redirectUri, and (if needed) clientSecret in web.config exactly match the values registered in your IdP's application settings. Even a tiny typo in the redirect URI will break authentication.
  • Ensure your app has the correct permissions set in the IdP (e.g., if you're accessing an API, make sure the app is granted access to that API's scopes).

If you're hitting a specific error (like a redirect loop, "invalid client" message, or missing user claims), share those details and we can narrow it down further!

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:22:40