该OWIN中间件的东西第三方登录集成到您的ASP.NET应用程序是非常酷的,但我似乎无法弄清楚如何就剜出来的新的ID,它取代了蹩脚的成员身份 API。我没有兴趣在坚持所产生的债权,并在英法为基础的数据持久化,我只是想这样我就可以把它应用到我的现有项目账户。我不希望采用新的编号只是为了利 用这些东西的优势。 我一直在浏览CodePlex上的代码,但有一大堆的静态magic。你可以提供什么建议吗?
本文地址 :CodeGo.net/608804/

-------------------------------------------------------------------------------------------------------------------------
1. 使用下面的代码来设置OWIN安全中间件:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Application",
AuthenticationMode = AuthenticationMode.Passive,
LoginPath = new PathString("/Login"),
LogoutPath = new PathString("/Logout"),
});
app.SetDefaultSignInAsAuthenticationType("External");
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "External",
AuthenticationMode = AuthenticationMode.Passive,
CookieName = CookieAuthenticationDefaults.CookiePrefix + "External",
ExpireTimeSpan = TimeSpan.FromMinutes(),
});
app.UseGoogleAuthentication();

上面的代码设置了应用程序的cookie,外部cookie和谷歌外部登录中间件。外部登录中间件将登录数据转换身份,并将其设置为外部cookie的中间件。在你的应用程序,你需要得到外部的cookie身份并将其转换为外部登录数据,那么你可以用它检查您的 下面是示例代码。 请使用应用程序的cookie:

var authentication = System.Web.HttpContext.Current.GetOwinContext().Authentication;
var identity = new ClaimsIdentity("Application");
identity.AddClaim(new Claim(ClaimTypes.Name, "<user name>"));
authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(identity, new AuthenticationProperties() {
IsPersistent = false
});

获取应用程序的cookie身份:

var identity = System.Web.HttpContext.Current.User as ClaimsIdentity;

获取外部的cookie身份(谷歌):

var authentication = System.Web.HttpContext.Current.GetOwinContext().Authentication;
var result = await authentication.AuthenticateAsync("External");
var externalIdentity = result.Identity;

从身份提取外部登录数据:

public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name)
};
}

最新文章

  1. Visual Studio 2015 显示正忙 正在等待内部操作完成
  2. 关于JavaScript lastIndexOf() 方法 w3school.com.cn写的不一定全对
  3. 关于http响应内容压缩的一点小积累。
  4. IOS 7 Study - UIDatePicker
  5. trident 序列号问题
  6. linux命令 --&gt; pwd命令
  7. 解读ECMAScript 6箭头函数
  8. java 类 及其 执行过程
  9. javascript同步分页
  10. android中activity.this跟getApplicationContext的区别
  11. 【LA 3027 Corporative Network】
  12. Python函数式实现单例特性
  13. httpClient4.5 closeableHttpClient用法
  14. 工具 docker
  15. FCC JS基础算法题(2):Check for Palindromes(检查回文字符串)
  16. js 数组与字符串的相互转化
  17. Ant Design of Angular
  18. 切换ubuntu-18.04启动方式
  19. Python爬虫教程-29-验证码识别-Tesseract-OCR
  20. 6.azkban的监控

热门文章

  1. Swift学习——Swift解释具体的基础(六)
  2. 【81.37%】【codeforces 734B】Anton and Digits
  3. Java背景
  4. 【37.00%】【vijos p1425】子串清除
  5. 如何控制WAP网站上输入框的默认键盘类型
  6. Windows系统的四个重要概念——进程、线程、虚拟内存、内核模式和用户模式
  7. 在服务器中无法自宿主启动一个webapi项目,引出查看系统端口是否被占用
  8. 随机森林算法原理及OpenCV应用
  9. 线程池;java的线程池的实现原理;适用于频繁互动(如电商网站)
  10. 键盘各键对应的编码值(key code)