public JsonResult GetUser(string uuid)
        {
            var(user, succeed, error) = this.identity.GetUserDetailsByID(uuid);
            if (user == null)
            {
                return(this.JsonFormat(false, JsonStatus.UserNotFound));
            }
            var content = new UserVM(user);
            var status  = JsonStatus.Succeed;

            if (user.IsThirdPart)
            {
                if (content.UserType == UserVMType.Weibo)
                {
                    // Load weibo user details. To extend this method if more 3rd-part added.
                    (succeed, error) = content.Create(this.identity.WeiboService.SearchWeiboUserInfoByUID).Check;
                    status           = succeed ? JsonStatus.Succeed : JsonStatus.WeiboDetailsNotFound;
                }
                else
                {
                    // No 3rd-part user infos found, action failed.
                    succeed = false;
                    status  = JsonStatus.ThirdPartUserNotExist;
                    error   = JsonErrorMaps.TryGet(status);
                }
            }
            return(this.JsonFormat(succeed, status, error, content.User));
        }
Пример #2
0
        private async Task GenerateToken(HttpContext context, JsonBase model, ProviderType type)
        {
            var param = new PostParams();

            if (context.Request.ContentType != "application/json")
            {
                if (type == ProviderType.Weibo)
                {
                    param.WeiboUid = context.Request.Form["wuid"];
                }
                else
                {
                    param.UserName = context.Request.Form["user"];
                    param.Password = context.Request.Form["psd"];
                }
            }
            else
            {
                var user = JsonHelper.FromJson <JwtProviderParam>(await GetContentAsync(context));
                if (type == ProviderType.Weibo)
                {
                    param.WeiboUid = user.WeiboUID;
                }
                else
                {
                    param.UserName = user.UserName;
                    param.Password = user.Password;
                }
            }

            context.Response.ContentType = "application/json";

            var scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            using (var scope = scopeFactory.CreateScope()) {
                this.identity = scope.ServiceProvider.GetRequiredService <IIdentityService <JwtSecurityToken, TokenProviderOptions, User> >();

                var(user, error) = GetIdentity(param, type);
                if (error != null)
                {
                    model.Message = error;
                    model.Status  = type == ProviderType.Register ? JsonStatus.RegisterFailed : JsonStatus.LoginFailed;
                    model.Content = new {
                        RedirectUrl = context.Request.Path
                    };
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(model, new JsonSerializerSettings {
                        ContractResolver      = new LowercaseContractResolver(),
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                        Formatting            = Formatting.Indented
                    }));

                    return;
                }

                model.Status  = JsonStatus.Succeed;
                model.Message = null;

                var vmProvider = new UserVM(user);
                if (type == ProviderType.Weibo)
                {
                    vmProvider = vmProvider.Create(this.identity.WeiboService.SearchWeiboUserInfoByUID);
                }

                model.Content = new TokenProviderVM {
                    Token   = new JwtSecurityTokenHandler().WriteToken(identity.GenerateToken(user, opts)),
                    Expires = (int)opts.Expiration.TotalSeconds,
                    Details = vmProvider.User
                };

                await context.Response.WriteAsync(JsonConvert.SerializeObject(model, new JsonSerializerSettings {
                    ContractResolver      = new LowercaseContractResolver(),
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    Formatting            = Formatting.Indented
                }));
            }
        }