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)); }
public static JsonResult JsonFormat( this Controller c, bool succeed, JsonStatus status = JsonStatus.Succeed, string error = default(string), dynamic content = default(dynamic)) => c.JsonFormat(succeed ? new JsonBase { Status = status, Message = c.GetErrorMessage(succeed, error ?? JsonErrorMaps.TryGet(status)), Content = content } : new JsonBase { Status = status == JsonStatus.Succeed ? JsonStatus.Error : status, Message = c.GetErrorMessage(succeed, error ?? JsonErrorMaps.TryGet(status == JsonStatus.Succeed ? JsonStatus.Error : status)), Content = content });
protected override async Task HandleChallengeAsync(AuthenticationProperties properties) { var authResult = await HandleAuthenticateOnceSafeAsync(); var eventContext = new JwtBearerChallengeContext(Context, Scheme, Options, properties) { AuthenticateFailure = authResult?.Failure }; // 避免返回错误 = invalid_token 如果错误不是由身份验证失败引起的 (例如, 缺少令牌)。 if (Options.IncludeErrorDetails && eventContext.AuthenticateFailure != null) { eventContext.Error = "invalid_token"; eventContext.ErrorDescription = CreateErrorDescription(eventContext.AuthenticateFailure); } await Events.Challenge(eventContext); if (eventContext.Handled) { return; } // 无论如何保证请求成功,用status表示具体api结果 Response.StatusCode = 200; if (string.IsNullOrEmpty(eventContext.Error) && string.IsNullOrEmpty(eventContext.ErrorDescription) && string.IsNullOrEmpty(eventContext.ErrorUri)) { Response.Headers.Append(HeaderNames.WWWAuthenticate, Options.Challenge); } else { // https://tools.ietf.org/html/rfc6750#section-3.1 //WWW 认证: 无记名领域 = "示例", 错误 = "invalid_token", error_description = "访问令牌过期" var builder = new StringBuilder(Options.Challenge); if (Options.Challenge.IndexOf(" ", StringComparison.Ordinal) > 0) { // 仅在第一个参数之后添加逗号 (如果有) builder.Append(','); } if (!string.IsNullOrEmpty(eventContext.Error)) { builder.Append(" error=\""); builder.Append(eventContext.Error); builder.Append("\""); } if (!string.IsNullOrEmpty(eventContext.ErrorDescription)) { if (!string.IsNullOrEmpty(eventContext.Error)) { builder.Append(","); } builder.Append(" error_description=\""); builder.Append(eventContext.ErrorDescription); builder.Append('\"'); } if (!string.IsNullOrEmpty(eventContext.ErrorUri)) { if (!string.IsNullOrEmpty(eventContext.Error) || !string.IsNullOrEmpty(eventContext.ErrorDescription)) { builder.Append(","); } builder.Append(" error_uri=\""); builder.Append(eventContext.ErrorUri); builder.Append('\"'); } Response.Headers.Append(HeaderNames.WWWAuthenticate, builder.ToString()); } await Response.WriteAsync(JsonConvert.SerializeObject(new JsonBase { Status = JsonStatus.BearerCheckFailed, Message = JsonErrorMaps.TryGet(JsonStatus.BearerCheckFailed) }, new JsonSerializerSettings { ContractResolver = new LowercaseContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented })); }