ASP.NET Core Razor Pages与Identity Server集成
创始人
2024-09-15 11:00:42
0

要将ASP.NET Core Razor Pages与Identity Server集成,可以按照以下步骤进行操作:

  1. 创建一个ASP.NET Core Razor Pages项目。可以使用Visual Studio或者命令行工具创建一个新的项目。

  2. 添加Identity Server NuGet包。在项目文件中的元素中添加以下内容:



  1. 创建Identity Server配置。在项目根目录中创建一个名为Config.cs的类文件,并添加以下内容:
using IdentityServer4.Models;
using System.Collections.Generic;

namespace YourProjectName
{
    public static class Config
    {
        public static IEnumerable IdentityResources =>
            new IdentityResource[]
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
            };

        public static IEnumerable ApiScopes =>
            new ApiScope[]
            {
                new ApiScope("api", "API"),
            };

        public static IEnumerable Clients =>
            new Client[]
            {
                new Client
                {
                    ClientId = "your-client-id",
                    ClientSecrets = { new Secret("your-client-secret".Sha256()) },

                    AllowedGrantTypes = GrantTypes.Code,
                    RequirePkce = true,
                    RequireClientSecret = false,

                    RedirectUris = { "https://localhost:5001/signin-oidc" },
                    PostLogoutRedirectUris = { "https://localhost:5001/signout-callback-oidc" },

                    AllowedScopes = { "openid", "profile", "api" },
                    AllowOfflineAccess = true,
                },
            };
    }
}

请将your-client-idyour-client-secret替换为实际的客户端ID和客户端密钥。

  1. 配置Identity Server服务。在Startup.cs文件中的ConfigureServices方法中添加以下代码:
services.AddIdentityServer()
    .AddInMemoryIdentityResources(Config.IdentityResources)
    .AddInMemoryApiScopes(Config.ApiScopes)
    .AddInMemoryClients(Config.Clients)
    .AddAspNetIdentity();

services.AddAuthentication()
    .AddIdentityServerJwt();

services.AddControllersWithViews();
services.AddRazorPages();
  1. 配置Identity Server中间件。在Startup.cs文件中的Configure方法中添加以下代码:
app.UseIdentityServer();

app.UseRouting();

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

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
});
  1. 创建一个用户管理页面。在Pages文件夹中创建一个名为Manage.cshtml.cs的类文件,并添加以下内容:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace YourProjectName.Pages
{
    [Authorize]
    public class ManageModel : PageModel
    {
        private readonly UserManager _userManager;
        private readonly SignInManager _signInManager;

        public ManageModel(UserManager userManager, SignInManager signInManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
        }

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

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

        public async Task OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound();
            }

            Input = new InputModel
            {
                Email = user.Email
            };

            return Page();
        }

        public async Task OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound();
            }

            user.Email = Input.Email;
            var result = await _userManager.UpdateAsync(user);
            if (result.Succeeded)
            {
                await _signInManager.RefreshSignInAsync(user);
                return RedirectToPage();
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            return Page();
        }
    }
}

该页面允许用户修改其电子邮件地址。

相关内容

热门资讯

盘点一款!德普之星透视辅助软件... 盘点一款!德普之星透视辅助软件,德扑之心免费透视,总是是有挂(有挂工具)暗藏猫腻,小编详细说明破解器...
我来教大家!如何下载wepok... 我来教大家!如何下载wepoker安装包,wepoker怎么获得好牌,真是真的是有挂(的确有挂);1...
重大消息!hhpoker是正规... 重大消息!hhpoker是正规的吗,wepoker破解器激活码,总是真的有挂(有挂教程)1、玩家可以...
记者发布!hhpoker德州透... 记者发布!hhpoker德州透视,pokermaster辅助器,一贯是有挂(新版有挂)1、游戏颠覆性...
分享实测!wepoker有没有... 分享实测!wepoker有没有插件,hhpoker德州挂真的有吗,本来真的是有挂(确实有挂)1、让任...
新手必备!pokemmo脚本辅... 新手必备!pokemmo脚本辅助器下载,智星菠萝辅助,真是是真的有挂(有挂教程)1、每一步都需要思考...
关于!wepoker可以透视码... 关于!wepoker可以透视码,wepoker数据分析工具,切实存在有挂(有挂辅助)1、首先打开辅助...
一分钟了解!微扑克辅助,wep... 一分钟了解!微扑克辅助,wepoker辅助器最新版本更新内容,好像真的是有挂(有挂解惑)一、wepo...
普及知识!wpk控制牌是真的吗... 普及知识!wpk控制牌是真的吗,aapoker脚本怎么用,都是真的有挂(确实有挂)亲,关键说明,透视...
玩家实测!epoker有透视吗... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...