看起来微软最终解决了这个问题。没有任何代码更改,它现在可以工作。
我现在有一个令牌和一个刷新令牌。
这就是我重定向到 Microsoft Live 的方式
Response.RedirectPermanent(string.Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&state={1}&response_type=code&redirect_uri={2}&scope=wl.signin%20wl.skydrive_update%20wl.offline_access", _microsoftClientId, userId, Url.AbsoluteAction("CallBack", "Microsoft")));
我的回调操作
public RedirectToRouteResult CallBack(string code, string state, string error, string error_description)
{
logger.Debug("Callback from Micrsoft");
if (string.IsNullOrWhiteSpace(error))
{
var client = new RestClient("https://login.live.com");
var request = new RestRequest("oauth20_token.srf", Method.POST);
request.AddParameter("grant_type", "authorization_code");
request.AddParameter("code", code);
request.AddParameter("client_id", _microsoftClientId);
request.AddParameter("client_secret", _microsoftClientSecret);
request.AddParameter("redirect_uri", Url.AbsoluteAction("CallBack", "Microsoft"));
logger.Debug("POSTING to Micrsoft");
var mslResponse = client.Execute<MicrosoftLiveResponse>(request);
if (mslResponse != null && mslResponse.Data != null)
{
logger.Debug("RESPONSE: " + mslResponse.Content);
var mslClient = mslResponse.Data;
if (string.IsNullOrWhiteSpace(mslClient.error))
{
//Update the database and redirect to the Done Action
if (OauthBL.UpdateMicrosoftLiveToken(Utilities.ConvertToObjectId(state, ObjectId.Empty), mslClient.access_token, mslClient.refresh_token, mslClient.expires_in))
{
return RedirectToAction("Done");
}
}
}
}
logger.Debug("INITIAL ERROR:" + error + " - " + error_description);
return RedirectToAction("Error");
}
还有我的 MicrosoftLiveResponse 实体
namespace Entities
{
public class MicrosoftLiveResponse
{
public string access_token { get; set; }
public string authentication_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string refresh_token { get; set; }
public string uid { get; set; }
public string error { get; set; }
public string error_description { get; set; }
}
}
希望这对某人有用