1

NET 5 が正式にリリースされたので、今晩 Net Core 3.1 から NET 5 に移行しました。アプリを実行しようとするまではすべてスムーズに進んでいるように見えましたが、startup.cs の 2 つの項目の下にいくつかの波線が見つかりました。 Microsoft Identity Web プラットフォームに関連付けられています。これは明らかに一瞬の失敗です!これを修正するまで、アプリを起動したり、Azure AD にログインしたりできません。

csproj ファイルを NET5 に変更した後、nuget マネージャーに移動し、すべてのパッケージを更新しました。

私はこの問題をどこから始めるべきかまったくわかりません:(

波線のある startup.cs ファイルのスクリーンショット:

startup.cs ファイル

csproj ファイル:

ここに画像の説明を入力

更新されたパッケージを含む Nuget Manager:

ここに画像の説明を入力

移行後、MS Identity Web の startup.cs ファイルの上部にあるパッケージ参照がグレー表示になっていることに気付きました。

ここに画像の説明を入力

startup.cs ファイルのコード:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public TokenValidatedContext Context { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Added to original .net core template.
        // ASP.NET Core apps access the HttpContext through the IHttpContextAccessor interface and 
        // its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor 
        // when you need access to the HttpContext inside a service.
        // Example usage - we're using this to retrieve the details of the currrently logged in user in page model actions.
        services.AddHttpContextAccessor();

        // DO NOT DELETE (for now...)
        // This 'Microsoft.AspNetCore.Authentication.AzureAD.UI' library was originally used for Azure Ad authentication 
        // before we implemented the newer Microsoft.Identity.Web and Microsoft.Identity.Web.UI NuGet packages. 
        // Note after implememting the newer library for authetication, we had to modify the _LoginPartial.cshtml file.
        //services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
        //    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

        ///////////////////////////////////

        // Add services required for using options.
        // e.g used for calling Graph Api from WebOptions class, from config file.
        services.AddOptions();

        // Sign-in users with the Microsoft identity platform
        services.AddSignIn(Configuration);

        // Token acquisition service based on MSAL.NET
        // and chosen token cache implementation
        services.AddWebAppCallsProtectedWebApi(Configuration, new string[] { GraphScopes.UserRead })
            .AddInMemoryTokenCaches();

        // Add the MS Graph SDK Client as a service for Dependancy Injection.
        services.AddGraphService(Configuration);

        // Create a new instance of the class that stores the methods called
        // by OpenIdConnectEvents(); i.e. when a user logs in or out the app.
        // See section below :- 'services.Configure'
        OpenIdEvents openIdEvents = new OpenIdEvents();

        // The following lines code instruct the asp.net core middleware to use the data in the "roles" claim in the Authorize attribute and User.IsInrole()
        // See https://docs.microsoft.com/aspnet/core/security/authorization/roles?view=aspnetcore-2.2 for more info.
        
        services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            // The claim in the Jwt token where App roles are available.
            options.TokenValidationParameters.RoleClaimType = "roles";
            // Advanced config - capturing user events. See OpenIdEvents class.
            options.Events ??= new OpenIdConnectEvents();
            options.Events.OnTokenValidated += openIdEvents.OnTokenValidatedFunc;
            // This is event is fired when the user is redirected to the MS Signout Page (before they've physically signed out)
            options.Events.OnRedirectToIdentityProviderForSignOut += openIdEvents.OnRedirectToIdentityProviderForSignOutFunc;
            // DO NOT DELETE - May use in the future.
            // OnSignedOutCallbackRedirect doesn't produce any user claims to read from for the user after they have signed out.
            options.Events.OnSignedOutCallbackRedirect += openIdEvents.OnSignedOutCallbackRedirectFunc;
        });

        // Adding authorization policies that enforce authorization using Azure AD roles. Polices defined in seperate classes.
        services.AddAuthorization(options =>
        {
            // This line may not work for razor at all, havent tried it but what was used in MVC from the MS Project example. Dont delete just yet...
            //options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));

            // NOTE BELOW - I had to change the syntax from RequireRole to RequireClaim
            options.AddPolicy(AuthorizationPolicies.AssignmentToEditRolesRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.EditRoles));
            options.AddPolicy(AuthorizationPolicies.AssignmentToViewLogsRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.ViewLogs));
            options.AddPolicy(AuthorizationPolicies.AssignmentToViewUsersRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.ViewUsers));
            options.AddPolicy(AuthorizationPolicies.AssignmentToCreateUsersRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.CreateUsers));
            options.AddPolicy(AuthorizationPolicies.AssignmentToEditUsersRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.EditUsers));
            options.AddPolicy(AuthorizationPolicies.AssignmentToDeleteUsersRoleRequired, policy => policy.RequireClaim(ClaimTypes.Role, AppRole.DeleteUsers));
        });

        services.AddRazorPages().AddMvcOptions(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        }).AddMicrosoftIdentityUI();

        // Add the HttpClient factory into our dependancy injection system.
        // That way we can access it at any point.
        // Used for consuming REST Api throughout the Webb App.
        services.AddHttpClient();
        // Adds the service for creating the Jwt Token used for calling microservices.
        // Note we are using our independant bearer token issuer service here, NOT Azure AD
        services.AddScoped<JwtService>();
        // Add service for HttpContext Current User Repository.
        // Used fir fetching properties of the currently logged in user for logging.
        services.AddScoped<ICurrentUser, CurrentUser>();

        // The AddAntiforgery() method configures anti-forgery service to pick the anti-forgery 
        // token from request headers rather than request body. This is required because we will 
        // be issuing Ajax requests to the razor page and there won't be any full page post-backs.
        services.AddAntiforgery(options => options.HeaderName = "MY-XSRF-TOKEN");
    }

これをトラブルシューティングする方法がわかりません...

4

1 に答える 1