问题描述
采用Asp.netIdentity2.0和asp.net4.5.1开发了一个网站,qq或是新浪微博用户用Oauth2登录网站后原来的本地用户就不能登录了.系统也不报错。但是只要把apppool回收一下就又可以登录了,有大侠知道是什么原因吗?代码如下:登录部份protectedvoidibQQLogin_Click(objectsender,System.Web.UI.ImageClickEventArgse){this.Session["RETURNURL"]=Request.QueryString["ReturnUrl"];varcontext=newQzoneContext();stringstate=Guid.NewGuid().ToString().Replace("-","");Session["qqrequeststate"]=state;stringscope="get_user_info";//stringscope="get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr";varauthenticationUrl=String.Format("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={0}&redirect_uri={1}&state={2}&scope={3}",QQOpenConnect.AppID,QQOpenConnect.callBackUrl,state,scope);Response.StatusCode=401;Response.Redirect(authenticationUrl);}callback回调:protectedvoidPage_Load(objectsender,EventArgse){if(!this.IsPostBack){if(Request.Params["code"]!=null){stringreturnUrl=this.Session["RETURNURL"]==null?"~/default.html":this.Session["RETURNURL"].ToString();varverifier=Request.Params["code"];varstate=Request.Params["state"];stringrequestState=this.Session["qqrequeststate"].ToString();if(state==requestState){varuserManager=newUserManager();StringuserName=String.Empty;try{UserAccessTokentoken=QQOpenConnect.GetAccess_Token(verifier,state);QQUserInfoqquerInfo=QQOpenConnect.GetQQUserInfo(token);lblUserName.Text=String.Format(@"欢迎,来自<imgsrc=""/Content/Images双击查看原图.png""/>QQ登录的 {0}!",qquerInfo.Nickname);ViewState["OpenID"]=token.OpenId;ViewState["AccessToken"]=token.AccessToken;ViewState["RefreshToken"]=token.RefreshToken;ViewState["NickName"]=qquerInfo.Nickname;foreach(ApplicationUserappuserinuserManager.Users){LBMProfileCommoneuserProfile=LBMProfileCommone.GetUserProfile(appuser.UserName);if(userProfile.QQOpenid==ViewState["OpenID"].ToString()){//更新用户AccessToken;//本来计划用session来传递登录类型,但跳转后session是空的//只要把登录类型写在profile里。但这有个问题,就是一个用户//同时在不同地点登录会有问题。(遗留问题)userProfile.LoginType="QQLogin";userProfile.QQAccesstoken=token.AccessToken;userProfile.QQRefreshtoken=token.RefreshToken;userProfile.FirstName=qquerInfo.Nickname;userProfile.Save();userName=appuser.UserName;break;}}}catch(Exceptionex){Response.Redirect("ExternalLoginError.aspx");}if(String.IsNullOrEmpty(userName)==false){varuser=userManager.FindByName(userName);IdentityHelper.SignIn(userManager,user,isPersistent:false);IdentityHelper.RedirectToReturnUrl(returnUrl,Response);}}elseResponse.Redirect("ExternalLoginError.aspx");}else{Response.Redirect("ExternalLoginError.aspx");}}}protectedvoidCreateUser_Click(objectsender,EventArgse){varmanager=newUserManager();stringreturnUrl=this.Session["RETURNURL"]==null?"~/default.html":this.Session["RETURNURL"].ToString();StringuserName=String.Format("QQ-{0}{1}{2}{3}{4}{5}",DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second,DateTime.Now.Millisecond,newRandom().Next(10000));varuser=newApplicationUser(){UserName=userName,Email=txtEmail.Text};IdentityResultresult=manager.Create(user,Password.Text);if(result.Succeeded){user=manager.FindByName(userName);if(manager.IsInRole(user.Id,Store_RoleName.StoreRegister.ToString())==false)manager.AddToRole(user.Id,Store_RoleName.StoreRegister.ToString());LBMProfileCommoneuserProfile=LBMProfileCommone.GetUserProfile(userName);userProfile.FirstName=ViewState["NickName"].ToString();userProfile.LoginType="QQLogin";userProfile.Mobile=btnMobilePhone.Text;userProfile.QQOpenid=ViewState["OpenID"].ToString();userProfile.QQAccesstoken=ViewState["AccessToken"].ToString();userProfile.QQRefreshtoken=ViewState["RefreshToken"].ToString();userProfile.Save();IdentityHelper.SignIn(manager,user,isPersistent:false);IdentityHelper.RedirectToReturnUrl(returnUrl,Response);Response.Redirect(returnUrl);}else{Response.Redirect("ExternalLoginError.aspx");}}这两部分的代码运行都没问题。