日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AspNet Identity 和 Owin 谁是谁

發布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AspNet Identity 和 Owin 谁是谁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

英文原文:http://tech.trailmax.info/2014/08/aspnet-identity-and-owin-who-is-who/?

最近我發現Stackoverflow上有一個非常好的問題.提問者問:為什么在調用AuthenticationManager.SignIn后,claim仍然可以被添加到Identity并持久化到cookie里.

示例代碼如下所示:

ClaimsIdentity identity = UserManager.CreateIdentity(user,
DefaultAuthenticationTypes.ApplicationCookie );
var claim1 = new Claim(ClaimTypes.Country, "Arctica"); identity.AddClaim(claim1);AuthenticationManager.SignIn(new AuthenticationProperties
{ IsPersistent = true }, identity );
var claim2 = new Claim(ClaimTypes.Country, "Antartica"); identity.AddClaim(claim2);

是的,為什么claim2在cookie已經設置完成后還可用.

在深入研究后,我發現AspNet Identity框架不設置cookie,而OWIN會設置,OWIN是Katana開源項目的一部分.有源碼可用是一件好事--你可以發現為什么事情有或沒有按你預期的方式工作.

在這個案例里,我花了一些時間探索Katana項目和?AuthenticationManager?工作方式.結果證明SignIn方法不設置cookie.它把Identity對象保存在內存里,直到設置響應cookies的時刻到來,然后claims被轉化為一個cookie,所有的事情就這樣魔法般地工作著 -)

結果證明Identity框架只處理user持久化,密碼哈希,驗證密碼是否正確,發送密碼重置郵件,等等.但是Identity實際上不驗證users或創建cookies.而Cookies是被OWIN處理的.

看一下登錄的代碼:

public async Task SignInAsync(Microsoft.Owin.Security.IAuthenticationManager
authenticationManager, ApplicationUser applicationUser, bool isPersistent) {authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);ClaimsIdentity identity = await UserManager.CreateIdentityAsync(applicationUser,
DefaultAuthenticationTypes.ApplicationCookie);authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties()
{
IsPersistent = isPersistent
}, identity); }

Identity只創建ClaimsIdentity(學習網站?ReferenceSource?),而ClaimsIdentity是.Net framework的一部分,而不是來自于互聯網的nuget包.然后這個ClaimsIdentity被傳給擁有一個設置cookies回調的OWIN的AuthenticationManager,而AuthenticationManager擁有一個在寫響應頭時設置cookies的回調.

到目前為止都很好,已有三部分:Identity框架創建一個ClaimsIdentity,OWIN根據這個ClaimsIdentity創建一個cookie,和.Net framework掌控ClaimsIdentity的類.

當在你的類中要訪問ClaimsPrincipal.Current時,你只用到.Net framework,不需要用到其它類庫,這是非常方便的!

?

默認的Claims

Identity框架為你做了一件很漂亮的事,默認情況下當你登錄時,它為一個principal添加了一些claims,如下所示:

  • User.Id:類型為“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier” 或ClaimTypes.NameIdentifier.

  • Username:類型為“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name” 或ClaimTypes.Name.

  • "ASP.NET Identity":保存為“http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“.這在你使用OpenId做驗證時非常有用.不過如果只是使用數據庫存儲users時沒什么用.點擊查看更多信息.

  • 包含user的安全郵戳的Guid,在Claim中持久化類型為“AspNet.Identity.SecurityStamp“.安全郵戳是user狀態的一個主要快照,如果驗證的密碼/方法,email,等等發生變化,安全郵戳就會發生變化,這允許你通過改變證書實現"在任何地方登出".從Kung的回答中獲取更多有關安全郵戳的信息.

  • 最有用的claims是role.所有分配給user的role被保存成ClaimTypes.Role或“http://schemas.microsoft.com/ws/2008/06/identity/claims/role“.所以下次你需要檢查當前user的roles,檢查這個claims,不會到數據庫中查找,這樣非常快.實際上,如果你調用ClaimsPrincipal的.IsInRole("RoleName"),框架會進入claims并檢查用戶是否分配了這個指定值的Role.

你可以在.Net Reference?網站查看這些claim類型,這個列表不是完整的,你可以創建你自己的claim類型--就是一個string.

如果你想添加你自己的owin claim類型,我建議你使用自己的符號,例如:“MyAppplication:GroupId”?,并保持所有的claim類型作為常量在一個類中:

public class MyApplicationClaimTypes {
? ? public string const GroupId = "MyAppplication:GroupId";
public string const PersonId = "MyAppplication:PersonId";
// other claim types

}


這種方式,你總是可以找到claims,并不會與框架中的claim類型沖突,除非你的claims與框架中的claims類型完全一致,例如:ClaimTypes.Email.

?

添加默認的claims

我總是在user登錄里,添加user的email到claims列表中,就如最前面示例里的claim1和claim2:

public async Task SignInAsync(IAuthenticationManager authenticationManager,
ApplicationUser applicationUser, bool isPersistent) {authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie,DefaultAuthenticationTypes.ApplicationCookie);
var identity = await this.CreateIdentityAsync(applicationUser,
DefaultAuthenticationTypes.ApplicationCookie);
// using default claim type from the frameworkidentity.AddClaim(new Claim(ClaimTypes.Email, applicationUser.Email));authenticationManager.SignIn(new AuthenticationProperties()
{ IsPersistent = isPersistent }, identity); }


你可以在這里為所有user添加默認的claims,但有一個IClaimsIdentityFactory類(賦給UserManager),只有一個方法:

public interface IClaimsIdentityFactory<TUser, TKey> where TUser :
? ? ? ? ? ?class, IUser<TKey> where TKey : IEquatable<TKey> {
? ? /// <summary>/// Create a ClaimsIdentity from an user using a UserManager ?
?
/// </summary>Task<ClaimsIdentity> CreateAsync(UserManager<TUser, TKey> manager,
TUser user, string authenticationType); }

?AspNet Identity的默認實現是:創建ClaimsIdentity,添加如上所述的默認claims,為user在數據庫中存儲IdentityUserClaims類型的claims.你可以重寫這個實現,并插入你自己的邏輯/claims:

public class MyClaimsIdentityFactory : ClaimsIdentityFactory<ApplicationUser, string> {
public override async Task<ClaimsIdentity> CreateAsync(UserManager<ApplicationUser, string>
userManager, ApplicationUser user, string authenticationType){
var claimsIdentity = await base.CreateAsync(userManager, user, authenticationType);claimsIdentity.AddClaim(new Claim("MyApplication:GroupId", "42"));
return claimsIdentity;} }

然后在賦給UserManger:

public UserManager(MyDbContext dbContext): base(new UserStore<ApplicationUser>(dbContext)) { // other configurations ? ?

// Alternatively you can have DI container to provide this class for better
application flexebility
this.ClaimsIdentityFactory = new MyClaimsIdentityFactory();
}

原文地址:http://www.cnblogs.com/kid1412/p/6403518.html


.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

總結

以上是生活随笔為你收集整理的AspNet Identity 和 Owin 谁是谁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色污污网站在线观看 | 中文字幕av在线免费 | 亚洲国产精品视频在线 | 国产手机在线视频 | 在线xxxx| 国产福利免费在线观看 | 最新av女优 | 红桃视频亚洲 | 都市激情亚洲色图 | 深爱综合网 | 国产精品15p | 天堂8在线天堂资源bt | 久久蜜桃av一区二区天堂 | 免费的毛片网站 | 日韩av线 | 欧美zzz物交| 国产精品99久久久久久www | 久久久久久久9 | 视频一区在线播放 | 嫩草懂你 | 999资源站 | 女生喷液视频 | 亚洲人午夜精品 | www.亚洲| 在线香蕉 | 亚洲区自拍 | 亚洲无码乱码精品国产 | h在线播放| 小嫩嫩精品导航 | 黑人干亚洲 | 成熟妇人a片免费看网站 | 久久久久久无码精品大片 | 国产精品一区在线播放 | 色婷婷av一区二区三区大白胸 | jizz一区二区 | 久久99久久久久久 | 搞中出| 制服丝袜影音先锋 | 久久精工是国产品牌吗 | 国产丝袜美女 | 国产乱码久久久久 | 永久免费视频网站 | 日韩精品在线观看免费 | 免费高清视频一区二区三区 | 中文字幕在线观看日韩 | 成人羞羞在线观看网站 | 精品久久福利 | 小sao货水好多真紧h无码视频 | 亚洲高清中文字幕 | 国产黄色免费大片 | 大黑人交xxx极品hd | 夜夜综合网 | 久久久亚洲国产 | 日韩区在线 | 夜夜爽妓女8888视频免费观看 | 少妇做爰免费视频播放 | 蜜桃又黄又粗又爽av免 | 亚洲区第一页 | 国产精品夫妻自拍 | 亚洲国产果冻传媒av在线观看 | 亚洲图片视频小说 | 亚洲色鬼 | 久久精品女人毛片国产 | 亚洲AV午夜福利精品一级无码 | 国产一卡二卡三卡 | 黄色精品一区二区 | 中文久久精品 | 日本国产高清 | 久久无码视频一区 | 麻豆网站在线看 | 91精品一区| 成人精品久久久 | 特级西西444www高清大胆免费看 | 一区二区国产在线 | 91看片国产 | 精品自拍一区 | 国产femdom调教7777| 一区二区三区免费播放 | 丁香六月色| 色涩涩 | 91成品视频 | 碧蓝之海动漫在线观看免费高清 | 亚洲精品国产电影 | 韩国成人免费视频 | 一区二区一级片 | 日本少妇做爰全过程毛片 | 人妻内射一区二区在线视频 | 99精品视频在线观看 | 女人的天堂av在线 | 成人午夜网址 | 91亚州 | 老司机午夜剧场 | 国产激情久久久久 | 精品一区二区三区在线免费观看 | av无线看 | 亚洲综合一区在线观看 | 欧美v视频 | 欧美深夜福利 | 狠狠的日|