NET+EF+MVC开发接口用户退出登陆接口和页面退出登陆实例,开发应用的时候都会遇到,笔者提供二个方法,以供参考:
一、接口 退出登陆方法
[HttpGet]
public HttpResponseMessage PA_ParentLogout(decimal ParentloginID, string UsrMp, string Token = null)
{
HttpResponseMessage result = null;
var ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"退出失败\",\"data\":\"\"}";
if (ParentloginID>0)
{
#region 验证token
if (!string.IsNullOrEmpty(Token))
{
int m = new WisdomStars.Bll.Api.Common().PUB_VerificationToken(Token);
if (m <= 0)
{
ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"登录失效\",\"data\":\"\"}";
result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
}
else
{
ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"token不能为空\",\"data\":\"\"}";
result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
#endregion 验证token
HashOperator operators1 = new HashOperator();
string key = Utils.GetDescription(KeyEnum.LoginUserCache.HXAppVaildParent) + UsrMp;
var ser = new ObjectSerializer();
ef.Configuration.ProxyCreationEnabled = false;
bool IsExist = operators1.Exist
if (IsExist == true)
{
//先删除redis中的key
operators1.Remove(key, key);
ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.成功 + "\",\"msg\":\"退出成功\",\"data\":\"\"}";
#region 再清空手机token
ef.HX_Parentlogin.Where(a => a.ParentloginID == ParentloginID && a.GuardianTel == UsrMp).Update(p => new HX_Parentlogin
{
mobiletoken = "",
client = 0
});
#endregion 清空手机token
}
else
{
ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.成功 + "\",\"msg\":\"退出成功\",\"data\":\"\"}";
#region 清空手机token
ef.HX_Parentlogin.Where(a => a.ParentloginID == ParentloginID && a.GuardianTel == UsrMp).Update(p => new HX_Parentlogin
{
mobiletoken = "",
client = 0
});
#endregion 清空手机token
}
}
result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
二、页面退出登陆方法,需要清除所有redis中的键值对。
#region 退出 + public ActionResult Logout()
public ActionResult Logout()
{
LoginInfo loginModel = Distribution.Common.Utils.GetAdminModel();
#region 获取登录信息
decimal adminUsrID = loginModel.adminUsrID;
var username = loginModel.Mobile.ToString();
#endregion 获取登录信息
if (adminUsrID > 0 && !string.IsNullOrEmpty(username))
{
#region 清除redis
HashOperator operators1 = new HashOperator();
var ser = new ObjectSerializer();
ef.Configuration.ProxyCreationEnabled = false;
HX_AdminUser m = ef.HX_AdminUser.Where(a=>a.adminUsrID== adminUsrID).AsNoTracking().FirstOrDefault();
string key = HX.Common.Utils.GetDescription(KeyEnum.LoginUserCache.HXDistributor) + username;
operators1.Remove(key);
string key2 = HX.Common.Utils.GetDescription(KeyEnum.LoginUserCache.HXDistributorCacheJurisdiction) + "_" + adminUsrID.ToString() + "_" + m.subsidiary_id.ToString();
operators1.Remove(key2);
#endregion 清除redis
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(aCookie);
return Redirect("/Home/Login");
}
else
{
return Redirect("/Home/Login");
}
}
#endregion 退出
留言与评论(共有 0 条评论) “” |