要将ASP.NET Core Razor Pages与Identity Server集成,可以按照以下步骤进行操作:
创建一个ASP.NET Core Razor Pages项目。可以使用Visual Studio或者命令行工具创建一个新的项目。
添加Identity Server NuGet包。在项目文件中的
元素中添加以下内容:
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-id
和your-client-secret
替换为实际的客户端ID和客户端密钥。
Startup.cs
文件中的ConfigureServices
方法中添加以下代码:services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.IdentityResources)
.AddInMemoryApiScopes(Config.ApiScopes)
.AddInMemoryClients(Config.Clients)
.AddAspNetIdentity();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
Startup.cs
文件中的Configure
方法中添加以下代码:app.UseIdentityServer();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
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();
}
}
}
该页面允许用户修改其电子邮件地址。