是否可以让 ServiceStack 的 FacebookAuthProvider 返回 AuthResponse 而不是总是返回 HttpWebResponse。我尝试创建自己的 CustomFacebookAuthProvider 并覆盖 Authenticate 方法。但无论我尝试什么,我都无法让它返回 AuthResponse。
甚至可能吗?
我试过这个:
public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
var tokens = Init(authService, ref session, request);
var code = authService.RequestContext.Get<IHttpRequest>().QueryString["code"];
var isPreAuthCallback = !code.IsNullOrEmpty();
if (!isPreAuthCallback)
{
var preAuthUrl = PreAuthUrl + "?client_id={0}&redirect_uri={1}&scope={2}"
.Fmt(AppId, this.CallbackUrl.UrlEncode(), string.Join(",", Permissions));
authService.SaveSession(session, SessionExpiry);
return authService.Redirect(preAuthUrl);
}
var accessTokenUrl = this.AccessTokenUrl + "?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}"
.Fmt(AppId, this.CallbackUrl.UrlEncode(), AppSecret, code);
HttpStatusCode statusCode;
var statusDescription = string.Empty;
var stackTrace = string.Empty;
try
{
var contents = accessTokenUrl.GetStringFromUrl();
var authInfo = HttpUtility.ParseQueryString(contents);
tokens.AccessTokenSecret = authInfo["access_token"];
session.IsAuthenticated = true;
authService.SaveSession(session, SessionExpiry);
OnAuthenticated(authService, session, tokens, authInfo.ToDictionary());
return new CustomAuthResponse
{
ReferrerUrl = session.ReferrerUrl,
ResponseStatus = new ResponseStatus(),
SessionId = session.Id,
UserName = session.UserName
};
}
catch (WebException we)
{
statusCode = ((HttpWebResponse)we.Response).StatusCode;
statusDescription = ((HttpWebResponse)we.Response).StatusDescription;
stackTrace = we.StackTrace;
if (statusCode == HttpStatusCode.BadRequest)
statusDescription = "AccessTokenFailed";
}
return new CustomAuthResponse
{
ReferrerUrl = session.ReferrerUrl,
ResponseStatus = new ResponseStatus
{
ErrorCode = statusCode.ToString(),
Message = statusDescription,
StackTrace = stackTrace
},
SessionId = session.Id,
UserName = session.UserName
};
}