ASP.NETCORE identity简化(隐形)模式 服务端

2025-03-18 19:59:53

1、1新垢卜埂呦增identity配置帮助类using System;using System.Collections.Generic;using System.Linq稆糨孝汶;using System.Threading.Tasks;using System.Collections;using System.Collections.Generic;using IdentityServer4.Models;using IdentityServer4.Test;namespace identityserverpeizhi{ public class IdentityConfig { //可访问的对象 public static IEnumerable<ApiResource> GetResource() { return new List<ApiResource> { new ApiResource("apiServer","apiServer") }; } public static IEnumerable<Client> GetClients() { return new List<Client> { new Client() { ClientId="clientId", AllowedGrantTypes = GrantTypes.Implicit,//隐式模式-------------------------------------------------------- ClientSecrets = { new Secret("secret1122".Sha512())}, AllowedScopes={ "apiServer" } //可以访问的resource } }; } //(1)添加用户配置 public static List<TestUser> GetTestUsers() { return new List<TestUser> { new TestUser { SubjectId ="111", Username ="test111", Password ="123456" } }; } public static IEnumerable<IdentityResource> GetIndentityReources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Email(), new IdentityResources.Profile() }; } }}

ASP.NETCORE identity简化(隐形)模式 服务端

2、1Startup.cs——configurationService配置 public void ConfigureServices(朐袁噙岿IServiceCollection services) { services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryClients(IdentityConfig.GetClients()) .AddInMemoryApiResources(IdentityConfig.GetResource()) .AddInMemoryIdentityResources(IdentityConfig.GetIndentityReources()) .AddTestUsers(IdentityConfig.GetTestUsers()); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); // Add application services. services.AddTransient<IEmailSender, EmailSender>(); services.AddMvc(); }

ASP.NETCORE identity简化(隐形)模式 服务端

3、1Startup.cs——Configure配置 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseIdentityServer();//---------------------------------------- app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }

ASP.NETCORE identity简化(隐形)模式 服务端

4、1登录设置,过期时间等等 public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) { if (_user.ValidateCredentials("test111", "123456")) { var user = _user.FindByUsername("test111"); var props = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(30)) }; await Microsoft.AspNetCore.Http.AuthenticationManagerExtensions.SignInAsync(HttpContext, user.SubjectId, user.Username, props); } return View(model); }

ASP.NETCORE identity简化(隐形)模式 服务端

5、完成

ASP.NETCORE identity简化(隐形)模式 服务端
猜你喜欢