这一篇主要讲创建房间以及用户登录拦截请求等
以下实现逻辑
///
/// 微信登陆请求
///
///
[HttpPost]
[SkipAuthorization]
[Route("Login")]
public Result Login()
{
string code = GetParams("code");
string xmlRes = WxPayAPI.HttpService.Get("https://api.weixin.qq.com/sns/jscode2session?appid=你得ID&secret=你得密钥&js_code" +
"=" + code + "&grant_type=authorization_code");
var xmlJson = JsonConvert.DeserializeObject>(xmlRes);
if (xmlJson != null && xmlJson.ContainsKey("session_key"))
{
string openId = xmlJson["openid"];
string sessionKey = xmlJson["session_key"];
string unionId = "";
if (xmlJson.ContainsKey("unionid"))
unionId = xmlJson["unionid"];
//创建或者更新用户
User user = _dbContext.Users.Where(o => o.Openid == openId).FirstOrDefault();
if (user == null)
{
//创建用户
User newU = new User()
{
Openid = openId
};
_dbContext.Users.Add(newU);
_dbContext.SaveChanges();
}
else
{
user.Openid = openId;
_dbContext.Users.Update(user);
_dbContext.SaveChanges();
}
//获取用户
user = _dbContext.Users.Where(o => o.Openid == openId).FirstOrDefault();
//更新或者插入session
SessionBag bag = SessionContainer.UpdateSession(null, openId, sessionKey,unionId);
UserReuslt reuslt = new UserReuslt()
{
User = user,
Key = bag.Key,
Openid = openId
};
return Result.Success(reuslt);
}
return Result.Fail("操作失败",new UserReuslt());
}
///
/// 登陆返回结果集
///
public class UserReuslt
{
public User User { get; set; }
public string Key { get; set; }
public string Openid { get; set; }
}
用户请求登录,带上code我们从微信服务器换取openid以及sessionKey,将sessionKey写入到SessionBag
封装了一个返回结果集,见下文
public class Result
{
internal const string DefaultExceptionMessage = "未知错误!";
///
/// 构造方法
///
public Result()
{
this.Code = HttpStatusCode.NotFound;
this.Msg = "操作失败";
this.Error = "";
this.Url = "";
}
///
/// 返回成功的对象
///
///
///
public Result Success(string msg)
{
if (string.IsNullOrEmpty(msg)) msg = "操作成功";
return new Result
{
Code = HttpStatusCode.OK,
Msg = msg,
Error = "",
Url = ""
};
}
///
/// 返回成功的对象
///
///
public static Result Success()
{
return Success(string.Empty);
}
///
/// 返回成功的带结果集的对象
///
///
///
///
///
public static Result Success(string msg, T data)
{
if (string.IsNullOrWhiteSpace(msg)) msg = "操作成功!";
return new Result
{
Code = HttpStatusCode.OK,
Msg = msg,
Error = "",
Url = "",
Data = data
};
}
///
/// 返回成功的带结果集的对象
///
///
///
///
public static Result Success(T data)
{
return Success(string.Empty, data);
}
///
/// 返回失败的带结果集的对象
///
///
///
///
///
public static Result Fail(string msg, T data)
{
if (string.IsNullOrWhiteSpace(msg)) msg = "操作失败!";
return new Result
{
Code = HttpStatusCode.BadRequest,
Msg = msg,
Error = "",
Url = "",
Data = data
};
}
///
/// 返回失败的带结果集的对象
///
///
///
///
public static Result Fail(string msg)
{
return Fail(msg, default);
}
///
/// 返回失败的对象
///
///
///
public static Result Fail(string msg)
{
if (string.IsNullOrWhiteSpace(msg)) msg = "操作失败!";
return new Result
{
Code = HttpStatusCode.BadRequest,
Msg = msg,
Error = "",
Url = ""
};
}
///
/// 状态码
///
public HttpStatusCode Code { get; set; }
///
/// 消息
///
public string Msg { get; set; }
///
/// 错误
///
public string Error { get; set; }
///
/// 错误Url
///
public string Url { get; set; }
}
public class Result : Result
{
private T _data = default;
///
/// 构造方法
///
public Result()
: base()
{
_data = default;
}
///
/// 操作结果业务数据
///
public T Data
{
get
{
if (typeof(T).BaseType == typeof(IEnumerator))
_data = Activator.CreateInstance();
return _data;
}
set
{
_data = value;
}
}
///
/// 返回成功的带消息提示对象
///
///
///
///
public static Result Success(string msg, T data)
{
return Success(msg, data);
}
///
/// 返回成功的带消息提示对象
///
///
///
public static Result Success(T data)
{
return Success(string.Empty, data);
}
///
/// 返回失败的带消息提示对象
///
///
///
///
public static Result Fail(string msg, T data)
{
return Fail(msg, data);
}
///
/// 返回失败的带消息提示对象
///
///
///
public static new Result Fail(string msg)
{
return Fail(msg, default);
}
}
以下为拦截器实现
public class ForeignAuthorize : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
//是否跳过验证
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true).
Any(o => o.GetType().Equals(typeof(SkipAuthorization))))
{
return;
}
var key = context.HttpContext.Request.Headers.TryGetValue("changqing-Header-Token", out var token);
if (!key)
{
context.Result = new JsonResult(Result.Fail("签名无效,此请求已被拒绝!"));
}
else
{
var bag = SessionContainer.GetSession(token.ToString());
if (bag == null || string.IsNullOrEmpty(bag.SessionKey))
{
context.Result = new JsonResult(Result.Fail("令牌无效,此请求已被拒绝!"));
}
}
//继续执行上下文
base.OnActionExecuting(context);
}
///
/// 跳过检测
///
public class SkipAuthorization : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
}
}
}
所有的控制器全部继承了一个重写的base控制器,在base里面进行注入
整个项目已上传至GitHub,觉得有用的同学麻烦点个star 谢谢。GitHub