微信 oauth2 兩次回調

 場景:api

logger.Info("f: " + wx.From);
logger.Info("c: " + wx.Code);
logger.Info("s: " + wx.State);app

Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=8RAN90uJ1967wpt1Y5&redirect_uri=www.xxxx.com%3a8001%2fWxAuthPage.aspx?from=notreserve&response_type=code&scope=snsapi_base&state=1#wechat_redirect");ui

出現:spa

除了code不一致,其它一致。3d

這樣致使以下兩次插入:code

var mealtake = new MealTake
{
Id = Guid.NewGuid(),
IsTaked = true,
TakeDate = DateTime.Now,
MealType = mt.MealType,
PersonOrder = personOrder
};blog

context.Set<MealTake>().Add(mealtake);
context.SaveChanges();oauth

 

想到的解決方法:date

Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=8RAN90uJ1967wpt1Y5&redirect_uri=www.xxxx.com%3a8001%2fWxAuthPage.aspx?from=notreserve&response_type=code&scope=snsapi_base&state=1#wechat_redirect");方法

改爲

Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=8RAN90uJ1967wpt1Y5&redirect_uri=www.xxxx.com%3a8001%2fWxAuthPage.aspx?from=notreserve&response_type=code&scope=snsapi_base&state="+Guid.NewGuid().ToString()+"#wechat_redirect");

 

再把插入代碼改爲

if (!context.Set<PersonOrder>().Any(n => n.Comment == wx.State))
{
  var person = context.Set<MealPerson>().Include("PersonType").FirstOrDefault(n => n.Id == wxUserInfo.User.Id);
  var personOrder = MealOrderService.CreatePersonOrder(person, mt.MealType, mealdate, false);
  personOrder.Comment = wx.State;
  var mealtake = new MealTake
  {
    Id = Guid.NewGuid(),
    IsTaked = true,
    TakeDate = DateTime.Now,
    MealType = mt.MealType,
    PersonOrder = personOrder
  };

  context.Set<MealTake>().Add(mealtake);
  context.SaveChanges();
}

=============================================================

具體緣由,原來是redirect_uri致使的,漏了對redirect_uri進行完整的UrlEncode:

redirect_uri=www.xxxx.com%3a8001%2fWxAuthPage.aspx?from=notreserve

改爲

redirect_uri=www.xxxx.com%3a8001%2fWxAuthPage.aspx%3ffrom%3dnotreserve

就不會發生兩次回調了

相關文章
相關標籤/搜索