asp.net identity overview

Post on 08-Jun-2015

942 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Gordon http://weisnote.blogspot.tw/

只要是網站十之八九都會有一套會員機制

2

先別管這個了 你有聽過 ASP.NET Membership 嗎?

4

something like...

限定 SQL Server 然後 schema 沒得改

圍繞著關聯式設計 不適用於 NoSQL

不能使用 OWIN

10

http://blog.darkthread.net/post-2013-12-01-about-owin.aspx

One ASP.NET Identity system ◦ ASP.NET MVC, Web Forms, Web Pages, Web API, SignalR 通吃

Ease of plugging in profile data about the user ◦ 易於擴充 profile data

Unit testability ◦ 易於單元測試

Role provider ◦ 支援角色管理

14

Claims Based ◦ 支援 Claims Based Identity Model (宣告式身分識別模型)

Social Login Providers ◦ 支援社群登入

Windows Azure Active Directory ◦ 可以用 Windows Azure AD 登入

OWIN Integration ◦ 整合 OWIN 把驗證放在 middleware

NuGet package ◦ 無腦安裝

15

16

有沒有一開專案就有會員功能的八卦?

17

18

19

20

21

22

/Models/IdentityModels.cs

25

/Models/RegisterViewModel.cs

26

/Views/Account/Register.cshtml

/Controllers/AccountController

28

29

30

每個網站的角色權限邏輯各異 自由發揮 來看看最常見的 Admin 怎麼玩

32

/App_Start/RoleConfig.cs

33

/Global.asax

34

35 /Controllers/AccountController.cs

36

有 Admin 角色的 User 可以連進 /admin/index 沒 Admin 角色的 User 會被導到登入頁 沒啥好看的

37

完全沒有畫面

臉書為例

取得應用程式編號 (appId) 取得應用程式密鑰 (appSecret) 新增平台 選 “網站” 設定 ”網站URL”

39

/App_Start/Startup.Auth.cs

40

41

以 MongoDB 為例

44

45

Controller

UserManager<TUser>

UserStore<TUser>

EntityStore<TEntity>

public void Create(TEntity entity) public void Delete(TEntity entity) public virtual Task<TEntity> GetByIdAsync(object id) public DbContext Context { get; private set; } public DbSet<TEntity> DbEntitySet { get; private set; } public IQueryable<TEntity> EntitySet { get; private set; }

internal class EntityStore<TEntity> where TEntity: class

46

Controller

UserManager<TUser> UserStore<TUser>

UserRepository

MongoHelper

UserClaim

UserLoginInfo

IdentityUser

ApplicationUser

/Models/IdentityModels.cs

47

/Controllers/AccountController.cs

48

/Models/IdentityUser.cs

49

50

/Models/UserClaim.cs

51

/Models/UserStore.cs ◦ IUserLoginStore<TUser> 對應使用者至登入提供者的介面

◦ IUserClaimStore<TUser> 儲存使用者指定宣告

◦ IUserRoleStore<TUser> 對應使用者至登入提供者的介面

◦ IUserPasswordStore<TUser> 儲存使用者的密碼雜湊

◦ IUserSecurityStampStore<TUser> 儲存使用者的安全性戳記

◦ IUserStore<TUser> 公開基本使用者管理的介面

52

https://mongolab.com/

53

/Models/MongoHelper.cs

54

55

/Models/UserRepository.cs

56

57

59

60

61

62

63

64

65

66

67

68

69

70

71

72

優點 ◦ 一秒生出會員登入 ◦ (部分)社群無痛登入 ◦ 一套 Identity通吃 ASP.NET MVC, Web Forms, Web Pages, Web API, SignalR

◦ 不是 SQL Server 也能用 ◦ 比較不會因為”人為因素” 被我的密碼沒加密 提名

缺點 ◦ 要學新東西 ˊ_>ˋ ◦ 架構不見得人人愛 ◦ UserManager 裡面的方法使用不當 效能會炸裂

◦ 部分 error message 是英文的 而且是 hard code

◦ 控制慾強烈的人無法忍受 ◦ 太新 有機率出現未爆彈

80

top related