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实现(微软)的更多相关文章

  1. .net mvc结合微软提供的FormsAuthenticationTicket登陆

    一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...

  2. [译]MVC网站教程(一):多语言网站框架

    本文简介 本博文介绍了 Visual Studio 工具生成的 ASP.NET MVC3 站点的基本框架:怎样实现网站的语言的国际化与本地化功能,从零开始实现用户身份认证机制,从零开始实现用户注册机制 ...

  3. [译]WebForms vs. MVC

    译者介绍 小小.NET学童,滴答…滴答…的雨…… 正文如下======================================================= 原文示例(VS2012): 1 ...

  4. ASP.Net MVC开发基础学习笔记:二、HtmlHelper与扩展方法

    一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...

  5. WebForms VS. MVC(翻译)

    (本文翻译自CodeProject上阿三写的一篇文章,原文地址:http://www.codeproject.com/Articles/528117/WebForms-vs-MVC,讲了有关ASP.A ...

  6. 使用 WPF+ ASP.NET MVC 开发 在线客服系统 (一)

    近段时间利用业余时间开发了一套在线客服系统,期间遇到过大大小小不少问题,好在都一一解决,最终效果也还可以,打算写一个系列的文章把开发过程详细的记录下来. 希望能够和更多的开发人员互相交流学习,也希望有 ...

  7. 6、ASP.NET MVC入门到精通——ASP.Net的两种开发方式

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 目前,ASP.NET中两种主流的开发方式是:ASP.NET Webform和ASP.NET MVC.从下图可以看到ASP.NET WebFo ...

  8. ASP.NET MVC 简介

    1. ASP.NET MVC 是什么? ASP.NET MVC是微软官方提供的以MVC模式为基础的ASP.NET Web应用程序(Web Application)框架,它由Castle的MonoRai ...

  9. ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法

    一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...

随机推荐

  1. JAVA基础2---深度解析A++和++A的区别

    我们都知道JAVA中A++和++A在用法上的区别,都是自增,A++是先取值再自增,++A是先自增再取值,那么为什么会是这样的呢? 1.关于A++和++A的区别,下面的来看个例子: public cla ...

  2. webpack4.0 实战记录

    从零配置webpack4.0 搭建React工程. 基本环境:Node(v8.1.2)+ webpack(v4.16.2) 1.在项目目录 命令窗口  执行  npm init 初始化项目,执行完后项 ...

  3. 获取当前exe的路径

    1.Assembly.GetExecutingAssembly().Location得到exe的全路径,Path.GetDirectoryName得到目录路径,不要用Directory.GetCurr ...

  4. springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法

    springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...

  5. mac电脑使用,开发环境配置指南

    mac电脑使用,开发环境配置指南 前端工具链,mac下都很好用 用brew来装软件 用brew cask来装应用 Introduction · macOS Setup Guidehttp://sour ...

  6. 1分钟完美安装最新CentOS+Nginx+PHP-FPM+MySQL

    PHP 5.3.1 MySQL 5.0.89 Nginx 0.8.33 或 0.7.65 (可选) 现在,我们可以快速全自动搞定 CentOS + Nginx + PHP-FPM + MySQL 的安 ...

  7. Python+OpenCV图像处理(二)——打印图片属性、设置图片存储路径、电脑摄像头的调取和显示

    一. 打印图片属性.设置图片存储路径 代码如下: #打印图片的属性.保存图片位置 import cv2 as cv import numpy as np #numpy是一个开源的Python科学计算库 ...

  8. css 初级进阶

    摘自:https://www.jianshu.com/p/dcc40ccc9841 CSS中级 Class和ID选择器 CSS初级教程中我们可以使用HTML标签选择器定义样式. 同样你也可以使用Cla ...

  9. GoldenGate实时投递数据到大数据平台(6)– HDFS

    GoldenGate可以实时将RDBMS的数据投递到HDFS中,在前面的文章中,已经配置过投递到kafka, mongodb等数据平台,本文通过OGG for bigdata的介质中自带的示例演示实时 ...

  10. 单例设计模式 --c#

    单例设计模式:在单例设计模式中我们要保持对象始终是唯一的 参考代码: class SingleObject { private SingleObject() { } private static Si ...