OAuth的MVC实现(微软)
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);
}
OAuth的MVC实现(微软)的更多相关文章
- .net mvc结合微软提供的FormsAuthenticationTicket登陆
一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...
- [译]MVC网站教程(一):多语言网站框架
本文简介 本博文介绍了 Visual Studio 工具生成的 ASP.NET MVC3 站点的基本框架:怎样实现网站的语言的国际化与本地化功能,从零开始实现用户身份认证机制,从零开始实现用户注册机制 ...
- [译]WebForms vs. MVC
译者介绍 小小.NET学童,滴答…滴答…的雨…… 正文如下======================================================= 原文示例(VS2012): 1 ...
- ASP.Net MVC开发基础学习笔记:二、HtmlHelper与扩展方法
一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...
- WebForms VS. MVC(翻译)
(本文翻译自CodeProject上阿三写的一篇文章,原文地址:http://www.codeproject.com/Articles/528117/WebForms-vs-MVC,讲了有关ASP.A ...
- 使用 WPF+ ASP.NET MVC 开发 在线客服系统 (一)
近段时间利用业余时间开发了一套在线客服系统,期间遇到过大大小小不少问题,好在都一一解决,最终效果也还可以,打算写一个系列的文章把开发过程详细的记录下来. 希望能够和更多的开发人员互相交流学习,也希望有 ...
- 6、ASP.NET MVC入门到精通——ASP.Net的两种开发方式
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 目前,ASP.NET中两种主流的开发方式是:ASP.NET Webform和ASP.NET MVC.从下图可以看到ASP.NET WebFo ...
- ASP.NET MVC 简介
1. ASP.NET MVC 是什么? ASP.NET MVC是微软官方提供的以MVC模式为基础的ASP.NET Web应用程序(Web Application)框架,它由Castle的MonoRai ...
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...
随机推荐
- 20165215 2017-2018-2《Java程序设计》第一周学习总结
20165215 2017-2018-2 <Java程序设计>第一周学习总结 教材学习内容总结 跟随网课学习了<Java2 实用教程>的第一章,进行了基础的编译练习 在Ubun ...
- Codeforce 513A - Game
Two players play a simple game. Each player is provided with a box with balls. First player's box co ...
- [转载]SQL Server中的事务与锁
了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...
- <转>jmeter(十)参数化
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- php学习路线(转)
作者:Summer链接:https://www.zhihu.com/question/20034403/answer/135433912来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- 怎样从外网访问内网WampServer?
本地安装了一个WampServer,只能在局域网内访问,怎样从外网也能访问到本地的WampServer呢?本文将介绍具体的实现步骤. 准备工作 安装并启动WampServer 默认安装的WampSer ...
- 如何通过命令行使用Wisdom RESTClient?
Wisdom RESTClient V1.2版本开始支持命令行方式运行. 工具地址: https://github.com/Wisdom-Projects/rest-client 使用说明:java ...
- pycharm+PyQt5+python最新开发环境配置,踩坑过程详解
安装工具:Pycharm 专业版2017.3PyQT5python3 pyqt5-tools 设置扩展工具的参数找到setting->tools->external tools,点击加号新 ...
- VMware环境安装MacOS
环境: win10专业版 VMware 14 Pro 开始吧 1. 停止服务 2. 解压并管理员权限运行unlocker,目的是使得 win10 环境下的 VMWare14Pro 支持 mac 系统的 ...
- 01:saltstack 基本使用
1.1 准备实验环境: 安装系统 1)硬件配置如下 2) 先把光标放到”install CentOS 7”,按 Tab键编辑内核参数,添加 (net.ifnames=0 biosdevname=0) ...