예제 #1
0
        private async Task <JsonWebToken> CreateJwtToken(User user, RequestClientType clientType = RequestClientType.Browser)
        {
            IServiceProvider  provider         = HttpContext.RequestServices;
            IJwtBearerService jwtBearerService = provider.GetService <IJwtBearerService>();
            JsonWebToken      token            = await jwtBearerService.CreateToken(user.Id.ToString(), user.UserName, clientType);

            return(token);
        }
예제 #2
0
 public static IBuildOperationLogicPipelineObject Create(RequestClientType type)
 {
     if (type.type == RequestClientType.App)
     {
         return(new ClientFotAppType());
     }
     return(null);
 }
 public static IBuildOperationLogicPipelineObject Create(RequestClientType clientType)
 {
     if (clientType.Type == RequestClientType.App)
     {
         return(new ClientTypeForAppType());
     }
     else if (clientType.Type == RequestClientType.NetClient)
     {
         return(new ClientForNet2Type());
     }
     return(null);
 }
예제 #4
0
 protected IActionResult DoJsonObjData <M>(Action <string, M> doData, RequestClientType requestClientType = RequestClientType.Web)
 {
     try
     {
         string json       = new StreamReader(Request.Body).ReadToEnd();
         M      qiNiuModel = JsonConvert.DeserializeObject <M>(json);
         doData(GetUiniacID(requestClientType), qiNiuModel);
         return(this.JsonSuccessStatus());
     }
     catch (ExceptionModel em)
     {
         return(this.JsonOtherStatus(em.ExceptionParam));
     }
     catch (Exception e)
     {
         e.Save();
         return(this.JsonErrorStatus());
     }
 }
예제 #5
0
        private void CheckUniacID(ref string uniacid, RequestClientType requestClientType, out IActionResult iar)
        {
            iar = null;
            switch (requestClientType)
            {
            case RequestClientType.Api:
                uniacid = UniacID;
                if (string.IsNullOrEmpty(uniacid))
                {
                    throw new ExceptionModel {
                              ExceptionParam = Tools.Response.ResponseStatus.uniacid为空
                    }
                }
                ;
                iar = null;
                break;

            case RequestClientType.Web:
                uniacid = GetSessionUniacID();
                if (string.IsNullOrEmpty(uniacid))
                {
                    iar = GoUserTimeOutPage();
                }
                break;

            case RequestClientType.Both:
                uniacid = GetSessionUniacID() ?? UniacID;
                if (string.IsNullOrEmpty(uniacid))
                {
                    iar = GoUserTimeOutPage();
                }
                break;

            default:
                iar = null;
                break;
            }
        }
예제 #6
0
 /// <summary>
 /// 创建指定用户的JwtToken信息
 /// </summary>
 /// <param name="userId">用户编号的字符串</param>
 /// <param name="userName">用户名的字符串</param>
 /// <param name="clientType">客户端类型</param>
 /// <returns>JwtToken信息</returns>
 public Task <JsonWebToken> CreateToken(string userId, string userName, RequestClientType clientType = RequestClientType.Browser)
 {
     return(CreateToken(userId, userName, clientType, null));
 }
예제 #7
0
        private async Task <JsonWebToken> CreateToken(string userId, string userName, RequestClientType clientType, RefreshToken refreshToken)
        {
            Check.NotNullOrEmpty(userId, nameof(userId));
            Check.NotNullOrEmpty(userName, nameof(userName));

            // New RefreshToken
            string clientId = refreshToken?.ClientId ?? Guid.NewGuid().ToString();

            Claim[] claims =
            {
                new Claim(ClaimTypes.NameIdentifier, userId),
                new Claim(ClaimTypes.Name,           userName),
                new Claim("clientId",                clientId),
                new Claim("clientType",              clientType.ToString())
            };
            var(token, expires) = CreateToken(claims, _jwtOptions, JwtTokenType.RefreshToken, refreshToken);
            string refreshTokenStr = token;
            await _provider.ExecuteScopedWorkAsync(async provider =>
            {
                IUnitOfWork unitOfWork          = provider.GetUnitOfWork(true);
                UserManager <TUser> userManager = provider.GetService <UserManager <TUser> >();
                refreshToken = new RefreshToken()
                {
                    ClientId = clientId, Value = refreshTokenStr, EndUtcTime = expires
                };
                IdentityResult result = await userManager.SetRefreshToken(userId, refreshToken);
                if (result.Succeeded)
                {
#if NET5_0
                    await unitOfWork.CommitAsync();
#else
                    unitOfWork.Commit();
#endif
                    IEventBus eventBus = _provider.GetService <IEventBus>();
                    OnlineUserCacheRemoveEventData eventData = new OnlineUserCacheRemoveEventData()
                    {
                        UserNames = new[] { userName }
                    };
                    await eventBus.PublishAsync(eventData);
                }
                return(result);
            });

            // New AccessToken
            IUserClaimsProvider claimsProvider = _provider.GetService <IUserClaimsProvider>();

            claims = await claimsProvider.CreateClaims(userId);

            List <Claim> claimList = claims.ToList();

            claimList.Add(new Claim("clientId", clientId));
            claimList.Add(new Claim("clientType", clientType.ToString()));
            (token, _) = CreateToken(claimList, _jwtOptions, JwtTokenType.AccessToken);

            return(new JsonWebToken()
            {
                AccessToken = token,
                RefreshToken = refreshTokenStr,
                RefreshUctExpires = expires.ToJsGetTime().CastTo <long>(0)
            });
        }