多语言展示
当前在线:1219今日阅读:27今日分享:41

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

ASP.NETCORE identity简化(隐形)模式 服务端
方法/步骤
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 GetResource()        {            return new List            {                new ApiResource('apiServer','apiServer')            };        }        public static IEnumerable GetClients()        {            return new List            {                new Client()                {                    ClientId='clientId',                    AllowedGrantTypes = GrantTypes.Implicit,//隐式模式--------------------------------------------------------                    ClientSecrets = { new Secret('secret1122'.Sha512())},                    AllowedScopes={ 'apiServer' } //可以访问的resource                }            };        }        //(1)添加用户配置        public static List GetTestUsers()        {            return new List            {                new TestUser                {                    SubjectId ='111',                    Username ='test111',                    Password ='123456'                }            };        }        public static IEnumerable GetIndentityReources()        {            return new List            {                new IdentityResources.OpenId(),                new IdentityResources.Email(),                new IdentityResources.Profile()            };        }    }}

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(options =>                options.UseSqlServer(Configuration.GetConnectionString('DefaultConnection')));            services.AddIdentity()                .AddEntityFrameworkStores()                .AddDefaultTokenProviders();            // Add application services.            services.AddTransient();            services.AddMvc();        }

4

1登录设置,过期时间等等        public async Task 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);        }

5

完成

推荐信息