dotnetopenauth - 更改Twitter的回拨网址?

时间:2011-01-28 20:54:56

标签: c# asp.net-mvc twitter dotnetopenauth

我正在使用dotnetopenauth库,我正在试图弄清楚如何更改回调网址。

我正在查看示例文件

public partial class SignInWithTwitter : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (TwitterConsumer.IsTwitterConsumerConfigured) {
                this.MultiView1.ActiveViewIndex = 1;

                if (!IsPostBack) {
                    string screenName;
                    int userId;
                    if (TwitterConsumer.TryFinishSignInWithTwitter(out screenName, out userId)) {
                        this.loggedInPanel.Visible = true;
                        this.loggedInName.Text = screenName;

                        // In a real app, the Twitter username would likely be used
                        // to log the user into the application.
                        ////FormsAuthentication.RedirectFromLoginPage(screenName, false);
                    }
                }
            }
        }

        protected void signInButton_Click(object sender, ImageClickEventArgs e) {
            TwitterConsumer.StartSignInWithTwitter(this.forceLoginCheckbox.Checked).Send();

        }

所以这就是向twitter发送请求所需的全部内容(对于mvc略有不同的webforms)。

我想更改响应返回的网址(我更喜欢单独的操作结果。

现在看来,StartSignInWithTwitter()就是设置网址的地方。

    /// <summary>
    /// Prepares a redirect that will send the user to Twitter to sign in.
    /// </summary>
    /// <param name="forceNewLogin">if set to <c>true</c> the user will be required to re-enter their Twitter credentials even if already logged in to Twitter.</param>
    /// <returns>The redirect message.</returns>
    /// <remarks>
    /// Call <see cref="OutgoingWebResponse.Send"/> or
    /// <c>return StartSignInWithTwitter().<see cref="MessagingUtilities.AsActionResult">AsActionResult()</see></c>
    /// to actually perform the redirect.
    /// </remarks>
    public static OutgoingWebResponse StartSignInWithTwitter(bool forceNewLogin) {
        var redirectParameters = new Dictionary<string, string>();
        if (forceNewLogin) {
            redirectParameters["force_login"] = "true";
        }
        Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_");
        var request = TwitterSignIn.PrepareRequestUserAuthorization(callback, null, redirectParameters);
        return TwitterSignIn.Channel.PrepareResponse(request);
    }

似乎是硬编码从上下文获取当前请求。如果没有我实际更改这行代码并重新编译.dll?

,是否有可能以某种方式覆盖它

修改 现在我对.dll进行了更改 - 我真的不喜欢这种方式,因为现在我需要支持自定义版本。

    public static OutgoingWebResponse StartSignInWithTwitter(bool forceNewLogin) {
  Uri callback =  MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_");
    return StartProcess(forceNewLogin, callback);
    }

private static OutgoingWebResponse StartProcess(bool forceNewLogin, Uri callback)
{
    var redirectParameters = new Dictionary<string, string>();
    if (forceNewLogin)
    {
        redirectParameters["force_login"] = "true";
    }
    var request = TwitterSignIn.PrepareRequestUserAuthorization(callback, null, redirectParameters);
    return TwitterSignIn.Channel.PrepareResponse(request);
}

public static OutgoingWebResponse StartSignInWithTwitter(bool forceNewLogin, Uri callback)
{
    return StartProcess(forceNewLogin, callback);
}

希望还有另一种方式。

1 个答案:

答案 0 :(得分:2)

我很感激你不想重新编译库的愿望。但是DotNetOpenAuth.ApplicationBlock库只是DotNetOpenAuth的一个辅助库,它附带了源代码,因为它实际上不是按原样使用。您可以将源代码复制并粘贴到您需要的Web应用程序中,然后转储其余部分。例如,应用程序块库中的版本之间没有向后兼容性承诺,就像核心dotnetopenauth.dll文件中那样。

所以,无论如何,如果您发现appblock存在缺陷,那么您就有权自行修复它。