从 asp net 2.2 迁移到 3.1 授权问题

Migrating from asp net 2.2 to 3.1 Authorization Issue

当我将网络应用程序从 asp 2.2 迁移到 3.1 时,我遇到了身份验证和授权问题。 看起来不错,并且在 CRUD 操作和其他方面表现正常。

在我的 SignIn.cshtml.cs 文件中我有这个:

public async Task<IActionResult> OnPostAsync()
{
    if (ModelState.IsValid)
    {
        var result = await signinManager.PasswordSignInAsync
        (SignInData.UserName, SignInData.Password,
            SignInData.RememberMe, false);
        if (result.Succeeded)
        {
            return RedirectToPage("../StartPage");
        }
        else
        {
            ModelState.AddModelError("", "Invalid login!");
        }
    }
    return Page();
}

然后我得到一个 result.Succeed 并且调用了到 StartPage 的重定向。

这是我的起始页

[Authorize(Roles = "Admin,Standardbruker")]
public class StartPageModel : PageModel
{
    public void OnGet()
    {

    }
}

但出于某种原因,我m not redirected to the start page. I仍然在登录页面上。

我知道这个 post 缺少信息,我可以 post 更多详细信息。

我已经按照这个Link to article,尽我所能

但我一定是漏掉了什么,我知道。

我正在使用 Razor Pages。

非常感谢任何帮助。

是的,在这里。

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NHA_Portal.Data;
using Microsoft.EntityFrameworkCore;
using NHA_Portal.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;



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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(loggingBuilder => {
                loggingBuilder.AddConsole()
                    .AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Information);
                loggingBuilder.AddDebug();
            });
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<AppIdentityDbContext>(options =>
             options.UseMySQL(Configuration.GetConnectionString("ManagementContext")));

            services.AddIdentity<AppIdentityUser, AppIdentityRole>()
                        .AddEntityFrameworkStores<AppIdentityDbContext>();

            services.ConfigureApplicationCookie(opt =>
            {
                opt.LoginPath = "/Security/SignIn";
                opt.AccessDeniedPath = "/Security/AccessDenied";
            });

            services.AddRazorPages();
            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            //services.AddDbContext<ManagementContext>(options => options.UseMySQL(Configuration.GetConnectionString("ManagementContext")));

            services.AddDbContext<ManagementContext>(options => {
                options.UseMySQL(Configuration.GetConnectionString("ManagementContext"));
                options.EnableSensitiveDataLogging(true);
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();
            

        }
    }
}

您需要输入您的代码:

app.UseAuthentication();

在你的app.UseAuthorization();

之前

像这样:

app.UseAuthentication();
app.UseAuthorization();