LoginController中:

第三方登陆

        public ActionResult LogOn()
{
string liveUrl =
string.Format(
"https://login.live.com/oauth20_authorize.srf?client_id={0}&scope=wl.Emails&response_type=code&redirect_uri={1}&locale={2}",
this.ClientId,
this.OAuthLogOnCallbackUrl,
this.Locale); return this.Redirect(liveUrl);
}

登陆成功,获取授权 

        public async Task<ActionResult> LogOnCallback()
{
string code = this.Request.QueryString["code"]; if (string.IsNullOrEmpty(code))
return RedirectToAction("Index", "Login"); string tokenUrl =
string.Format(
"https://login.live.com/oauth20_token.srf?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code&locale={4}",
this.ClientId,
this.OAuthLogOnCallbackUrl,
this.ClientSecret,
code,
this.Locale); string liveId = string.Empty;
try
{
liveId = await RequestLiveIdByToken(await RequestToken(tokenUrl));
}
catch (Exception e)
{
_logger.Fatal("无法获取LiveId Token", e);
var result = new ViewModels.LoginResult
{
Success = false,
ErrorMessage = "无法连接登录服务,请稍后再试。"
};
return View("Index", result);
} if (!string.IsNullOrEmpty(liveId))
{
var userSvc = _userSvc;
if (userSvc.CurrentUser == null)
{
UserInfo user = userSvc.GetUserByEmail(liveId); if (user != null && user.IsEnable)
{
return this.DoLogin(user);
}
else
{
var result = new ViewModels.LoginResult
{
Success = false
}; if (user != null && !user.IsEnable)
{
result.ErrorMessage = "用户被禁止登录!";
}
else
{
result.ErrorMessage = "用户不存在!";
} return View("Index", result);
}
} return this.DoLogin(userSvc.CurrentUser);
} return this.RedirectToAction("Index", "Login");
}
        [NonAction]
private async Task<string> RequestToken(string url)
{
var request = WebRequest.Create(url); using (var response = await request.GetResponseAsync())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
var json = sr.ReadToEnd();
return JsonConvert.DeserializeAnonymousType(json, new { access_token = "" }).access_token;
}
}
} [NonAction]
private async Task<string> RequestLiveIdByToken(string token)
{
if (string.IsNullOrEmpty(token))
return string.Empty; var request = WebRequest.Create(string.Format("https://apis.live.net/v5.0/me?access_token={0}", token));
using (var response = await request.GetResponseAsync())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
string json = sr.ReadToEnd();
var userJson = JsonConvert.DeserializeAnonymousType(json, new { emails = new { account = "" } });
return userJson.emails.account;
}
}
}

注销登陆 

        public ActionResult LogOff()
{
this.PreLogout();
string liveUrl =
string.Format(
"https://login.live.com/oauth20_logout.srf?client_id={0}&scope=wl.Emails&response_type=code&redirect_uri={1}&locale={2}",
this.ClientId,
this.OAuthLogOnCallbackUrl,
this.Locale); return this.Redirect(liveUrl);
}

  

最新文章

  1. WinAPI—— CallNextHookEx调用下一个钩子
  2. Java NIO与IO的差别和比較
  3. 【Deep Learning学习笔记】Dynamic Auto-Encoders for Semantic Indexing_Mirowski_NIPS2010
  4. python 全栈开发之路 day1
  5. Sql Server 2008清理数据库日志的语句
  6. 《Programming WPF》翻译 第5章 6.触发器
  7. 【DataStructure】Some useful methods about linkedList(二)
  8. zoj1025 Wooden Sticks
  9. linux 让一个程序开机自启动并把一个程序加为服务
  10. PopupWindow 的使用
  11. CCF-CSP 最大的矩形
  12. CentOS下mysql数据库data目录迁移和配置优化
  13. Dynamic 中修改实体中主字段的长度
  14. Python-数据类型之字典
  15. Oracle synonym 同义词
  16. 如何高效的学习 TensorFlow 代码?
  17. 每天一个linux命令(17):whereis
  18. lua C++ wrapper
  19. Git Flow 工作模型与使用
  20. influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB

热门文章

  1. Python大神成长之路: 第二次学习记录
  2. Presto 学习
  3. qt之qmake
  4. 关于spark进行实时日志解析,保存hbase与mysql
  5. echarts遇到的问题
  6. jt项目日志查询流程
  7. maven项目启动报错;class path resource [com/ssm/mapping/] cannot be resolved to URL because it does not exist
  8. ltp makefile 解析
  9. Hbuilder安装
  10. JDK源码之ThreadLocal