如何确保 ASP.NET Core Razor Pages 中的一段代码在身份验证后立即执行?

问题描述 投票:0回答:1

我正在使用 ASP.NET Core 8 Identity 我想执行一些代码,以确保刚刚登录的当前用户已指定他们希望使用的时区。如果没有,我想将他们重定向到询问其时区的页面。如果他们未指定时区,则不应通过身份验证来使用该应用程序。

起初,我想到只是在 Razor Page 代码隐藏方法中执行此代码,并在身份验证后重定向到该页面,但这意味着所有用户只需输入正确的 URL,他们就可以自由使用应用程序正常。

如何在对用户进行身份验证后执行这些检查,同时保留其身份验证状态,直到他们完成指定时区的任务?

asp.net-core authentication razor-pages asp.net-core-identity
1个回答
0
投票

我已经使用中间件实现了这个功能,你可以查看下面的详细步骤。

1。创建一个

TimeZoneMiddleware.cs
文件并注册。

using Microsoft.AspNetCore.Identity;
using TimeZoneApp.Data;

namespace TimeZoneApp
{
    public class TimeZoneMiddleware
    {
        private readonly RequestDelegate _next;

        public TimeZoneMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context, UserManager<ApplicationUser> userManager)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                if (!context.Request.Path.StartsWithSegments(new PathString("/TimeZoneApp")))
                {
                    var user = await userManager.GetUserAsync(context.User);
                    if (user != null && string.IsNullOrEmpty(user.TimeZone))
                    {
                        context.Response.Redirect("/TimeZoneApp");
                        return;
                    }
                }
            }

            await _next(context);
        }
    }
}

2。注册一下

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

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();
// add it here
app.UseMiddleware<TimeZoneMiddleware>();

app.MapRazorPages();

app.Run();

3.我的测试页

@page
@model TimeZoneApp.Pages.SetTimeZoneModel

<h2>Set Your Time Zone</h2>

<form method="post">
    <label for="timezone">Time Zone:</label>
    <select asp-for="UserTimeZone" class="form-control">
        <option value="">Select a time zone</option>
        <option value="UTC-05:00">Eastern Time (US & Canada)</option>
        <option value="UTC-08:00">Pacific Time (US & Canada)</option>
        <!-- Add more time zones as needed -->
    </select>
    <button type="submit" class="btn btn-primary">Set Time Zone</button>
</form>



using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TimeZoneApp.Data;

namespace TimeZoneApp.Pages
{
    [Authorize]
    public class SetTimeZoneModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;

        public SetTimeZoneModel(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }

        [BindProperty]
        public string UserTimeZone { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user != null)
            {
                user.TimeZone = UserTimeZone;
                var result = await _userManager.UpdateAsync(user);
                if (result.Succeeded)
                {
                    return RedirectToPage("/Index");
                }
            }
            return Page();
        }
    }
}

4。应用程序用户.cs

using Microsoft.AspNetCore.Identity;

namespace TimeZoneApp.Data
{
    public class ApplicationUser : IdentityUser
    {
        public string? TimeZone { get; set; }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.