iml6yu.IdentityService.Client 1.1.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package iml6yu.IdentityService.Client --version 1.1.2
                    
NuGet\Install-Package iml6yu.IdentityService.Client -Version 1.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="iml6yu.IdentityService.Client" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="iml6yu.IdentityService.Client" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="iml6yu.IdentityService.Client" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add iml6yu.IdentityService.Client --version 1.1.2
                    
#r "nuget: iml6yu.IdentityService.Client, 1.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package iml6yu.IdentityService.Client@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=iml6yu.IdentityService.Client&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=iml6yu.IdentityService.Client&version=1.1.2
                    
Install as a Cake Tool

iml6yu.IdentityService.Client

认证类库客户端 ,支持多种认证方式,支持webapi和桌面应用程序。

license

MIT License

Copyright (c) 2021 iml6yu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

webapi的用法

  • 注入认证中间件
            //初始化配置信息
            IdentityServiceClientOption? idOption = builder.Configuration.GetSection("IdentityService").Get<IdentityServiceClientOption>();
            if(idOption != null)
            {
                //注入配置
                builder.Services.AddIdentityClient(idOption);
            }
  • 启用认证
        //启用认证
        app.UseIdentityClient(); 
  • 配置信息
   //认证客户端配置
  "IdentityService": {
    //认证类型 0 账号密码;1 客户端模式(就是密钥授权);2 授权码模式
    "IdentityType": 0,
    //客户端ID
    "ClientId": "xxxxx",
    //客户端密钥
    "ClientSecret": "xxxx",
    //认证地址
    "AuthorityHost": "http://xxxxxx:8500",
    //作用域 取决于服务端给分配的作用域(一般一个项目的作用域是固定不变的)
    "Scope": [ "iml6yu_test_api" ],
    //是否开启Https验证 如果true则必须用https协议和证书
    "RequireHttpsMetadata": false,
    //认证框架名称,默认就是Bearer,可以根据自己需要进行设置
    "AuthenticationScheme": "Bearer",
    //授权码类型的配置信息,其他类型的不用配置
    "AuthorizationCode": {
      //重定向URL,就是伴随code回传给服务器认证后,服务器重定向的url
      "RedirectUri": "xxx",
      //Token携带方式,伴随重定向url携带的token,默认是cookie。可以设置 0 在url的查询字符串中,1在cookie中,2 在header中
      "TokenParameterType": 1, 
      //获取code模式登录的地址
      "SignInUrl": "url",
      //错误页面
      "ErrorPage": "url"
    }

客户端用法

  • 授权码方式
            IdentityServiceClient clientManager;
            var option = new IdentityServiceClientOption()
            {
                AuthorityHost = "https://localhost:5001",
                ClientId = "xxxxx",
                ClientSecret = "xxxx",
                Scope = new List<string> { "xxxxx" },
                IdentityType = IdentityType.AuthorizationCode,
                Code = new AuthorizationCode()
                {
                    RedirectUri = "https://localhost:5004"
                }
            };

            var bearTokenValidator = new BearerTokenValidator(Microsoft.Extensions.Logging.LoggerFactory.Create(config => {
                config.AddConsole();
            }).CreateLogger<BearerTokenValidator>());

            clientManager = new IdentityServiceClient(option, bearTokenValidator);

            var result = await clientManager.LoginLocalAppWithoutApiServerAsync();
            if (!result.State)
                MessageBox.Show(result.Message + "\r\n--------------------------------------------\r\n" + result.Error.ToString());

            else
                MessageBox.Show(result.Datas?.AccessToken + "\r\n" + result.Datas?.RefreshToken + "\r\n" + result.Datas?.IdentityToken + "\r\n" + result.Datas?.Scope);
      
  • 账号密码方式
            IdentityServiceClient clientManager;
            var option = new IdentityServiceClientOption()
            {
                AuthorityHost = "https://localhost:5001",
                ClientId = "xxxx",
                ClientSecret = "xxxx",
                Scope = new List<string> { "xxxx" },
                IdentityType = IdentityType.ResourceOwerPassword
            };

            var bearTokenValidator = new BearerTokenValidator(Microsoft.Extensions.Logging.LoggerFactory.Create(config => {
                config.AddConsole();
            }).CreateLogger<BearerTokenValidator>());

            clientManager = new IdentityServiceClient(option, bearTokenValidator);
            var result = await clientManager.LoginAsync(new ResourceOwerPassword()
            {
                Password = "iml6yu.Admin",
                UserName = "admin"
            });

            if (!result.State)
                MessageBox.Show(result.Message + "\r\n--------------------------------------------\r\n" + result.Error.ToString());

            else
                MessageBox.Show(result.Datas.AccessToken + "\r\n" + result.Datas.RefreshToken + "\r\n" + result.Datas.IdentityToken + "\r\n" + result.Datas.Scope);

  • 密钥方式登录
  IdentityServiceClient clientManager;
            var option = new IdentityServiceClientOption()
            {
                AuthorityHost = "https://localhost:5001",
                ClientId = "xxxxxxxx",
                ClientSecret = "xxxxx",
                Scope = new List<string> { "xxxx" },
                IdentityType = IdentityType.ClientCredentials
            }; 
            clientManager = new IdentityServiceClient(option);
            var result = await clientManager.LoginAsync();

            if (!result.State)
                MessageBox.Show(result.Message + "\r\n--------------------------------------------\r\n" + result.Error?.ToString());

            else
                MessageBox.Show(result.Datas.AccessToken + "\r\n" + (result.Datas.RefreshToken ?? "no RefreshToken!!!") + "\r\n" + result.Datas.IdentityToken + "\r\n" + result.Datas.Scope);

Api 中用法

在controller中注入IdentityServiceClient

  • 例子
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : BaseController<LoginController>
    {
        IdentityServiceClient idClient;

        public LoginController(BearerTokenValidator bearerTokenValidator, ILogger<LoginController> logger, IdentityServiceClient idClient) : base(bearerTokenValidator, logger)
        {
            this.idClient = idClient;
        }
        [AllowAnonymous]
        [HttpGet]
        public async Task<DataResult<TokenResponse>> Login(string username, string password)
        {
            var result = await idClient.LoginAsync(new ResourceOwerPassword() { Password = password, UserName = username });
            return result;
        }
      }

AppBrowserScheme(.net6 ^+^ )

通过注册自定义浏览器协议,可以唤起本地app。 目前仅支持windows

注册框架

  AppBrowserScheme.RegistBrowserScheme("框架名称,默认是iml6yu");

注销框架

    AppBrowserScheme.UnRegistBrowserScheme("框架名称,默认是iml6yu");

最后

如果在使用过程中遇到任何bug或者困难,可以添加WeChat或者是Microsoft Teams

Wechat Microsoft Teams
WeChat Join Us
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0-beta260224001 101 2/24/2026
1.2.0-beta260208002 112 2/8/2026
1.1.2 145 12/28/2025
1.1.1 230 10/26/2025
1.0.2-beta250403001 227 4/2/2025
1.0.2-beta250402001 217 4/2/2025