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

.NET 7.0 Web页面无法与控制器交互,登录注册功能失效求助

.NET 7.0 Web项目登录/注册前端对接故障排查

我开发了一个.NET 7.0 Web项目,后端功能已完成并通过Swagger测试全部正常,但对接前端页面后,登录、注册功能无法正常工作。相关代码如下:

IAuthService接口

public interface IAuthService
{
    Task<User> RegisterAsync(UserRequestDto request);
    Task<User> LoginAsync(UserRequestLogDto request);
}

AuthService实现

using Microsoft.EntityFrameworkCore;
using Ryzhanovskyi.University.Tinder.Core.Interfaces;
using Ryzhanovskyi.University.Tinder.Models.Auth;
using Ryzhanovskyi.University.Tinder.Models.Models;
using Ryzhanovskyi.University.Tinder.Web.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ryzhanovskyi.University.Tinder.Core.Services
{
    public class AuthService : IAuthService
    {
        private readonly DataContext _context;
        private readonly IEmailSender _emailSender;

        public AuthService(DataContext context, IEmailSender emailSender)
        {
            _context = context;
            _emailSender = emailSender;
        }

        public async Task<User> RegisterAsync(UserRequestDto request)
        {
            if (await _context.Users.AnyAsync(u => u.Email == request.Email))
            {
                return null; 
            }

            string passwordHash = BCrypt.Net.BCrypt.HashPassword(request.Password);

            var user = new User
            {
                UserName = request.Username,
                Email = request.Email,
                PasswordHash = passwordHash
            };

            var welcomeEmail = new EmailModel
            {
                Email = request.Email,
                Subject = "Mail for successfully registration at GnomeLove",
                Message = $"Welcome {user.UserName} to Dating Web APP GnomeLove, good luck in searching your love ^_^"
            };
            await _emailSender.SendEmailAsync(welcomeEmail.Email, welcomeEmail.Subject, welcomeEmail.Message);

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return user; 
        }

        public async Task<User> LoginAsync(UserRequestLogDto request)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.Email == request.Email);

            if (user == null)
            {
                return null;
            }

            if (!BCrypt.Net.BCrypt.Verify(request.Password, user.PasswordHash))
            {
                return null;
            }

            return user;
        }
    }
}

AuthController控制器

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Ryzhanovskyi.University.Tinder.Core.Interfaces;
using Ryzhanovskyi.University.Tinder.Models.Auth;
using Ryzhanovskyi.University.Tinder.Models.Models;
using Ryzhanovskyi.University.Tinder.Core.Services;

namespace Ryzhanovskyi.University.Tinder.Web.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly IAuthService _authService;

        public AuthController(IAuthService authService)
        {
            _authService = authService;
        }

        [HttpPost("register")]
        public async Task<ActionResult<User>> Register(UserRequestDto request)
        {
            var user = await _authService.RegisterAsync(request);

            if (user == null)
            {
                return BadRequest("Email is already registered.");
            }

            return Ok(user);
        }

        [HttpPost("login")]
        public async Task<ActionResult<User>> Login(UserRequestLogDto request)
        {
            var user = await _authService.LoginAsync(request);

            if (user != null)
            {
                return user;
            }

            if (user == null)
            {
                return BadRequest("Invalid email or password.");
            }

            return Ok(user);
        }
    }
}

Login.cshtml前端页面

@page
@using Ryzhanovskyi.University.Tinder.Models.Auth
@model Ryzhanovskyi.University.Tinder.Web.Pages.Auth.LoginModel

@{
    ViewData["Title"] = "Login";
}

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6 card p-4">
            <h2 class="text-center text-info mb-4">Login</h2>

            <form asp-action="Login" method="post">
                <div asp-validation-summary="ModelOnly" class="mb-3"></div>

                <div class="mb-3">
                    <label asp-for="UserRequestLog.Email"></label>
                    <input asp-for="UserRequestLog.Email" class="form-control" />
                    <span asp-validation-for="UserRequestLog.Email" class="text-danger"></span>
                </div>

                <div class="mb-3">
                    <label asp-for="UserRequestLog.Password"></label>
                    <input type="password" asp-for="UserRequestLog.Password" class="form-control" />
                    <span asp-validation-for="UserRequestLog.Password" class="text-danger"></span>
                </div>

                <div class="d-flex justify-content-between align-items-center">
                    <a href="Register" class="text-decoration-none">Don't have an account?</a>
                    <input type="submit" value="Login" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts{
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
}

Login.cshtml.cs页面模型

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Ryzhanovskyi.University.Tinder.Core.Interfaces;
using Ryzhanovskyi.University.Tinder.Models.Auth;

namespace Ryzhanovskyi.University.Tinder.Web.Pages.Auth
{
    public class LoginModel : PageModel
    {
        private readonly IAuthService _authService;

        public LoginModel(IAuthService authService)
        {
            _authService = authService;
        }

        [BindProperty]
        public UserRequestLogDto UserRequestLog { get; set; }

        public async Task<IActionResult> LoginAsync()
        {
            if (ModelState.IsValid)
            {
                var result = await _authService.LoginAsync(UserRequestLog);

                if (result != null)
                {
                    return RedirectToPage("/main");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Wrong Password or Account with this email doesn't exist");
                    return Page();
                }
            }

            return Page();
        }
    }
}

我是首次接触.NET技术,老师要求开发该项目但未提供指导,尝试多种方法仍无法解决问题,求排查解决思路。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:35:09