无法重定向到ASP点网络核心中的本地页面

问题描述

我是asp.net core的新手。我已经使用Identity进行登录注册。成功登录后,我试图重定向到我的页面

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl = Url.Content("~/Home/Index");

        if (ModelState.IsValid)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout,set lockoutOnFailure: true
            var result = await _signInManager.PasswordSignInAsync(Input.Email,Input.Password,Input.RememberMe,lockoutOnFailure: false);
            if (result.Succeeded)
            {
                _logger.Loginformation("User logged in.");
                return LocalRedirect(returnUrl);
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToPage("./LoginWith2fa",new { ReturnUrl = returnUrl,RememberMe = Input.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            else
            {
                ModelState.AddModelError(string.Empty,"Invalid login attempt.");
                return Page();
            }
        }

        // If we got this far,something Failed,redisplay form
        return Page();
    }

下面是我的启动文件,我在其中进行了一些配置

Startup.cs

 public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseAuthentication();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }

更新1

下面是我的Login.cshtml.cs

namespace DemoLogin.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public class LoginModel : PageModel
{
    private readonly UserManager<DemoLoginUser> _userManager;
    private readonly SignInManager<DemoLoginUser> _signInManager;
    private readonly ILogger<LoginModel> _logger;

    public LoginModel(SignInManager<DemoLoginUser> signInManager,ILogger<LoginModel> logger,UserManager<DemoLoginUser> userManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _logger = logger;
    }

    [BindProperty]
    public InputModel Input { get; set; }

    public IList<AuthenticationScheme> ExternalLogins { get; set; }

    public string ReturnUrl { get; set; }

    [TempData]
    public string ErrorMessage { get; set; }

    public class InputModel
    {
        [required]
        [EmailAddress]
        public string Email { get; set; }

        [required]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public async Task OnGetAsync(string returnUrl = null)
    {
        if (!string.IsNullOrEmpty(ErrorMessage))
        {
            ModelState.AddModelError(string.Empty,ErrorMessage);
        }

        returnUrl = returnUrl ?? Url.Content("~/");

        // Clear the existing external cookie to ensure a clean login process
        await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

        ReturnUrl = returnUrl;
    }

    public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");

        if (ModelState.IsValid)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout,redisplay form
        return Page();
    }
}
}

我已经注册一个用户,然后尝试登录。但是我没有重定向到我的页面~/Home/Index

解决方法

我想我知道问题所在,您是否将const maxSubequence = (arr) => { let i,j; let msis = arr.slice(); //Copy input array //result variables let resultSeq = []; let maxSum = 0; let result = []; var bookmark = new Array(arr.length); bookmark[0] = -1; /* Compute maximum sum values in bottom up manner */ for (i = 1; i < arr.length; i++) { bookmark[i] = -1; for (j = 0; j < i; j++) { if (arr[i] > arr[j] && msis[i] < msis[j] + arr[i]) { msis[i] = msis[j] + arr[i]; bookmark[i] = j; } } } // console.log(tempSeq); /* Pick maximum of all msis values */ let maxIndex = 0; for (i = 0; i < msis.length; i++) { if (maxSum < msis[i]) { maxSum = msis[i]; maxIndex = i; } } resultSeq = [arr[maxIndex]]; for (i = bookmark[maxIndex]; i >= 0; i = bookmark[i]) { resultSeq.unshift(arr[i]) } result = [maxSum,resultSeq]; return result; } console.log( maxSubequence([30,20,10,50]) )属性添加到了Index方法中?

这是解决方案。

将顺序更改为:

[Authorize]

炫耀: enter image description here

由于您的项目中存在很多错误,因此建议您创建一个新项目并按照我的步骤进行。这很简单。

第一步:(更改 app.UseAuthentication(); app.UseAuthorization(); ):

enter image description here

第二步:(单击项目Authentication,然后单击Add。)

enter image description here

Step3 :(选择“身份”,然后添加):

enter image description here

Step4(覆盖所有文件,然后添加):

enter image description here

最后:迁移并运行项目。首先注册,然后登录。