简体   繁体   English

使用没有密码的 IdentityServer3 生成访问令牌

[英]Generate access token with IdentityServer3 without password

How to manually generate access_token from server without password?如何在没有密码的情况下从服务器手动生成 access_token? I want to allow super admins login as users and look at their problems and see the problems by their eyes, so i need user access_token.我想允许超级管理员以用户身份登录并查看他们的问题并通过他们的眼睛看到问题,所以我需要用户 access_token。 i already see this question but didn't help me in IdentityServer3.我已经看到这个问题,但在 IdentityServer3 中没有帮助我。

first create a custom grant named loginBy首先创建一个名为 loginBy 的自定义授权

    public class LoginByGrant : ICustomGrantValidator
    {
        private readonly ApplicationUserManager _userManager;

        public string GrantType => "loginBy";

        public LoginByGrant(ApplicationUserManager userManager)
        {
            _userManager = userManager;
        }     

        public async Task<CustomGrantValidationResult> ValidateAsync(ValidatedTokenRequest request)
        {

            var userId = Guid.Parse(request.Raw.Get("user_id"));

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
                return await Task.FromResult<CustomGrantValidationResult>(new CustomGrantValidationResult("user not exist"));

            var userClaims = await _userManager.GetClaimsAsync(user.Id);

            return
                await Task.FromResult<CustomGrantValidationResult>(new CustomGrantValidationResult(user.Id.ToString(), "custom", userClaims));

        }
    }

then add this custom grant in identity startup class然后在身份启动类中添加此自定义授权

    factory.CustomGrantValidators.Add(
                        new Registration<ICustomGrantValidator>(resolver => new LoginByGrant(ApplicaionUserManager)));

and finally in your api最后在你的 api 中

      public async Task<IHttpActionResult> LoginBy(Guid userId)
       {
        var tokenClient = new TokenClient(Constants.TokenEndPoint, Constants.ClientId, Constants.Secret);

        var payload = new { user_id = userId.ToString() };

        var result = await tokenClient.RequestCustomGrantAsync("loginBy", "customScope", payload);

        if (result.IsError)
            return Ok(result.Json);

        return Ok(new { access_token = result.AccessToken, expires_in = result.ExpiresIn});
       }

this is for identityServer3 but for identityServer4 it is pretty similar这是针对 identityServer3 但对于 identityServer4 非常相似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IdentityServer3通过请求后的用户名和密码获取用户令牌 - IdentityServer3 get user token by username and password post REQUEST 在注销时不会删除IdentityServer3令牌 - IdentityServer3 Token Not Getting Deleted on Logout 为什么identityserver3没有返回刷新令牌? - Why identityserver3 did not return refresh token? 使用IdentityServer3 + Entity Framework进行令牌验证 - Token validation with IdentityServer3 + Entity Framework 如何使用自定义用户服务为identityserver3添加访问令牌的声明 - How to add claims to access token for identityserver3 using custom user service identityserver3 - 一个身份服务器发布的访问令牌可以与另一台身份服务器一起使用吗? - identityserver3 - can an access token issued by one id server be used with another id server? 密码流的 IdentityServer4 客户端不包括访问令牌中的 TestUser 声明 - IdentityServer4 client for Password Flow not including TestUser claims in access token 如何为IdentityServer3 grant_type:password创建其他参数 - How to create additional parameters for IdentityServer3 grant_type:password IdentityServer3连接/令牌端点始终返回401:未授权 - IdentityServer3 connect/token endpoint always return 401: unauthorized 在IdentityServer3中使用刷新令牌时获取NullReferenceException - Getting NullReferenceException when using refresh token in IdentityServer3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM