準備工作:
依賴項:microsoft.aspnetcore.authentication.jwtbearer
用于建立jwt密匙和釋出資訊,放在appsetting.json中,注入依賴iconfiguration時選擇microsoft.extensions.configuration
依賴項:microsoft.aspnetcore.identity.entityframeworkcore
用于身份認證,擷取使用者等identity操作。架構内包含signinmanager、usermanager
使用方法:
首先先要建立使用者類,我們選擇繼承identity.entityframeworkcore提供的identityuser類建立新的使用者類,該使用者類下以address作為标記,綁定購物車、訂單、角色集合,後續還會添加token等資訊
之後建立jwthelper類,通過identity架構擷取使用者資訊并為之建立token
appsetting中的資訊如下:
<code>{"authentication": {"secretkey": "suibianzifuchaun","issuer": "fakexiecheng.com","audience": "fakexiecheng.com"}}</code>
3. 登入和注冊需要用到的兩個操作
①驗證使用者和密碼:
②建立使用者對象并存儲
完整的登入和注冊方法代碼:
服務注入(全程在startup中)
1、注入jwt的認證服務
2、啟動使用者授權架構