IdentServer2 使用不同方式实现(swagger+client+html+postman)
IdentServer2 使用不同方式实现(swagger+client+html+postman)
IdentServer登陆校验
Swagger 校验登陆方式
1. Implicit
2. Password
C#使用IdentServer例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | using IdentityModel.Client; using System; using System.Net.Http; namespace TPL.Client { class Program { static void Main( string [] args) { ClientModel(); Console.WriteLine(); PassWordModel(); Console.ReadKey(); } /// <summary> /// 客户端模式 /// </summary> static void ClientModel() { Console.WriteLine( "=== ClientModel === " ); var client = new HttpClient(); var response_token = client.RequestClientCredentialsTokenAsync( new ClientCredentialsTokenRequest { Address = "http://localhost:5100/connect/token" , ClientId = "api_client" , ClientSecret = "secret" , Scope = "api" }).Result; Console.WriteLine( "=============================Token==============================" ); Console.WriteLine(response_token.AccessToken ?? response_token.Error); Console.WriteLine( "=============================WebApi==============================" ); // 调用API client.SetBearerToken(response_token.AccessToken); var responses_api1 = client.GetAsync( "http://localhost:5001/api/Main/HelloString" ).Result; var content1 = responses_api1.Content.ReadAsStringAsync().Result; Console.WriteLine(content1); //var responses_api2 = client.GetAsync("http://localhost:5001/api/Main/HelloString").Result; //var content2 = responses_api2.Content.ReadAsStringAsync().Result; //Console.WriteLine(content2); } static void PassWordModel() { Console.WriteLine( "=== PassWordModel === " ); var client = new HttpClient(); var response = client.RequestPasswordTokenAsync( new PasswordTokenRequest { Address = "http://localhost:5100/connect/token" , ClientId = "apiClientPassword" , ClientSecret = "apiSecret" , Scope = "api" , UserName = "system" , Password = "system" }).Result; Console.WriteLine( "=============================Token==============================" ); Console.WriteLine(response.AccessToken ?? response.Error); Console.WriteLine( "=============================WebApi==============================" ); // 调用API client.SetBearerToken(response.AccessToken); var responses = client.GetAsync( "http://localhost:5001/api/Main/HelloString" ).Result; if (response.IsError) { Console.WriteLine(response.HttpStatusCode); } else { //Console.WriteLine("=============================服务返回值=============================="); var content = responses.Content.ReadAsStringAsync().Result; Console.WriteLine(content); } } } } |
Html使用IdentServer例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | < html > < head > < title >identsever_test</ title > </ head > < body > < div id="newplay"></ div > < script src="jquery-1.10.2.min.js"></ script > < script type="text/javascript"> // localStorage.setItem('uid','10001'); // var uid = localStorage.getItem('uid'); // localStorage.removeItem('uid'); // localStorage.clear(); // 遍历 // localStorage.uid = 1; // localStorage.sex='nan'; // localStorage.age = 23; // for ( var i = 0, len = localStorage.length; i < len ; ++i ) { // console.log( localStorage.key( i ) +':' + localStorage.getItem(localStorage.key( i )) ); //} var token = "http://localhost:5100/connect/token"; var siteinfo = "http://localhost:5001/api/"; identity_init(); function identity_init() { var data = { username: "system", password: "system", grant_type: 'password', client_id: 'apiClientPassword', client_secret: 'apiSecret' }; $.ajax({ url: token, method: 'POST', data: data, header: { "content-type": "application/x-www-form-urlencoded" }, success(res) { console.log("get token success:"); console.log(res); var access_token = res.access_token; localStorage.setItem('access_token', access_token); }, fail(res) { console.log(res); }, complete(res) { console.log(res); } }) var access_token = localStorage.getItem('access_token'); $.ajax({ url: siteinfo + "Main/HelloString", method: 'GET', beforeSend: function (request) { request.setRequestHeader("Authorization", "Bearer " + access_token); }, success(res) { console.log("get webapi success:"); console.log(res); }, fail(res) { console.log(res) }, complete(res) { console.log(res); } }) } </script> </ body > |
PostMan使用IdentServer例子
代码结构
qq:505645074
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步