此Twitter登录页面(使用LinqToTwitter)按预期工作,直到用户登录,然后注销,然后再次登录同一会话。
此时语句(代码片段底部)
_auth.BeginAuthorization(returnURI);
似乎已执行,但未执行重定向。
任何人都有这种经历或有建议,我很难过。
任何想法的TIA!
代码:
protected void Page_Load(object sender, EventArgs e)
{
_credentials = new SessionStateCredentials();
if (_credentials.ConsumerKey == null || _credentials.ConsumerSecret == null)
{
_credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
_credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
}
_auth = new WebAuthorizer
{
Credentials = _credentials,
PerformRedirect = authUrl => Response.Redirect(authUrl)
};
if (!IsPostBack)
{
if (Request.QueryString["act"] != null)
{
string act = Request.QueryString["act"].ToString();
switch (act)
{
case "login":
_auth.CompleteAuthorization(Request.Url);
// we do record tokens in SQL here but code removed
// for this exercise
//log us in for FormsAuthentication
FormsAuthentication.SetAuthCookie(_auth.Credentials.ScreenName, false);
string ret = Request.QueryString["ret"];
Response.Redirect(HttpUtility.UrlDecode(ret));
break;
case "logout":
FormsAuthentication.SignOut();
_credentials = null;
_auth = null;
Response.Redirect("~/");
break;
}
}
else
{
if (_auth != null)
{
_return = Request.QueryString["ret"];
string OAuthReturnUrl = String.Format("http://mydomain.com/login?act=login&ret={0}&ts={1}", HttpUtility.UrlEncode(_return), DateTime.Now);
Uri returnURI = new Uri(OAuthReturnUrl);
/******************************************
on second login in same browser session this executes
and return URI is correct but no
redirect takes place. ???
******************************************/
_auth.BeginAuthorization(returnURI);
}
}
}
}
答案 0 :(得分:2)
我的不好......当然,SessionCredentials使用会话状态:
if (_auth != null)
{
_return = Request.QueryString["ret"];
string OAuthReturnUrl = String.Format("http://myDomain.com/login?act=login&ret={0}&ts={1}", HttpUtility.UrlEncode(_return), DateTime.Now);
Uri returnURI = new Uri(OAuthReturnUrl);
_credentials.AccessToken = null;
_credentials.OAuthToken = null;
_credentials.ScreenName = "";
_auth.Credentials = _credentials;
_auth.BeginAuthorization(returnURI);
}
重置以前获得的令牌并允许重定向。