| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Senparc.Weixin.MP.AdvancedAPIs;
- using Senparc.Weixin;
- using Microsoft.AspNetCore.Http;
- using Senparc.Weixin.Entities;
- using Microsoft.Extensions.Options;
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
- namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
- {
- [Route("api/[controller]")]
- public class AuthController : BaseController
- {
- public AuthController(IOptions<SenparcWeixinSetting> senparcWeixinSetting) : base(senparcWeixinSetting)
- {
- }
- [HttpGet]
- public ActionResult Get(string code, string state, string returnUrl)
- {
- if (string.IsNullOrEmpty(code))
- {
- return Content("您拒绝了授权!");
- }
- if (!state.Contains("|"))
- {
- //这里的state其实是会暴露给客户端的,验证能力很弱,这里只是演示一下
- //实际上可以存任何想传递的数据,比如用户ID
- return Content("验证失败!请从正规途径进入!1001");
- }
- try
- {
- //通过,用code换取access_token
- var openIdResult = OAuthApi.GetAccessToken(appId, appSecret, code);
- if (openIdResult.errcode != ReturnCode.请求成功)
- {
- return Content("错误:" + openIdResult.errmsg);
- }
- HttpContext.Session.SetString("OpenId", openIdResult.openid);//进行登录
- //也可以使用FormsAuthentication等其他方法记录登录信息,如:
- //FormsAuthentication.SetAuthCookie(openIdResult.openid,false);
- return Redirect(returnUrl);
- }
- catch (Exception e)
- {
- return Content("错误:" + e.Message);
- }
- }
- }
- }
|