基本上,我正在嘗試創建一個可以在應用程式中添加/洗掉產品的管理員角色(canEdit)。分析“IdentityResult Create”擴展方法,我發現它有 1 覆寫。
- 一個有引數:manager,TUser user
- 另一個有引數:manager、TUser用戶、字串密碼
這是我在 Wingtip Toys Microsoft asp.net 教程結束時不斷遇到的錯誤。在我的代碼中,除了應用程式的名稱(如你所見,我的是游戲商店)外,我的代碼與教程相同,我認為我必須使用第二種方法,因為管理員角色有密碼。
但是我嘗試了不同的方法,并通過分析所有淺藍色類和介面等的元資料擴展來理解和解決這個問題,但我根本無法解決這個問題。
編輯
我正在添加代碼。下面的粗體線是錯誤線。
namespace GameStore.Logic
{
internal class RoleActions
{
internal void AddUserAndRole()
{
// Access the application context and create result
variables.
Models.ApplicationDbContext context = new
ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("canEdit"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name =
"canEdit" });
}
var userMgr = new UserManager<ApplicationUser>(new
UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
};
**IdUserResult = userMgr.Create(appUser,
ConfigurationManager.AppSettings["AppUserPasswordKey"]);**
if(!userMgr.IsInRole(userMgr.FindByEmail("
[email protected]").Id, "canEdit"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("
[email protected]").Id, "canEdit");
}
}
}
}
以下是此 Admin 檔案夾的組態檔,但如您所見,沒有涉及密碼。
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="canEdit"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
uj5u.com熱心網友回復:
你期望在哪里
ConfigurationManager.AppSettings["AppUserPasswordKey"]
從那里獲取密碼?您沒有在組態檔中輸入值以供讀取。
不得不說,把新用戶的密碼放在ap config檔案里會很奇怪。
我的意思是你說'看看這里沒有密碼',好吧,那么為什么你的代碼試圖從那里讀取密碼?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466308.html
