OAuth的MVC实现(微软)
2021-05-23 20:31
标签:rem else his erb tps bsp ble mes 登录 LoginController中: 第三方登陆 登陆成功,获取授权 注销登陆 OAuth的MVC实现(微软) 标签:rem else his erb tps bsp ble mes 登录 原文地址:http://www.cnblogs.com/panpanwelcome/p/7682832.html 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 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
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);
}