问题描述
我正在编写一个新的应用程序,当我最初使用该模板创建项目时,它包含了ASP.NET提供的默认用户管理功能。我对此并不十分熟悉,并且很难找到不属于
MVC应用程序教程的文档。
我用过了几个教程,并有一个扩展的ApplicationRole类。
公共类ApplicationRole:IdentityRole
{
public ApplicationRole( ):base(){}
public ApplicationRole(string name):base(name){}
public string说明{get;组; }
}
在教程和模板中有一个IdentityConfig.cs文件,它有各种类,包括一个用于ApplicationUserManager的文件,我使用代码创建了一个ApplicationRoleManager类下面:
公共类ApplicationRoleManager:RoleManager< ApplicationRole>
{
public ApplicationRoleManager(
IRoleStore< ApplicationRole,string> roleStore)
:base(roleStore)
{
}
public static ApplicationRoleManager创建(
IdentityFactoryOptions< ApplicationRoleManager>选项,IOwinContext上下文)
{
返回新的ApplicationRoleManager(
new RoleStore< ApplicationRole>(context.Get< ApplicationDbContext>()));
}
}
问题来自第一步:创建新角色。我正在尝试在创建角色之前检查角色是否存在,并认为下面的代码(在.aspx.cs文件中)可行,但我不断获得异常(粘贴在代码下面)
protected void CreateRoleButton_Click(object sender,EventArgs e)
{
string newRoleName = RoleName.Text.Trim();
var manager = Context.GetOwinContext()。GetUserManager< ApplicationRoleManager>();
if(!manager.RoleExists(newRoleName))
{
var role = new ApplicationRole(newRoleName);
manager.Create(role);
}
RoleName.Text = string.Empty;
}
$
例外:
'/'应用程序中的服务器错误。
值不能为空。
参数名称:manager
描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.ArgumentNullException:值不能为null。
参数名称:manager
来源错误:
第29行:
第30行:
第31行:if( manager.RoleExists(newRoleName))
第32行:{
第33行:
源文件:C:\ Users \Administrator \Source \ Repos\QuickQuote \QuickQuote \QuickQuote \Roles \ManageRoles.aspx.cs Line:31
Stack Trace:
[ArgumentNullException:Value不能为空。
参数名称:manager]
Microsoft.AspNet.Identity.RoleManagerExtensions.RoleExists(RoleManager`2 manager,String roleName)+155
QuickQuote.Roles.ManageRoles.CreateRoleButton_Click(Object sender,EventArgs e )在C:\ Users \Administrator \Source\Repos\QuickQuote \QuickQuote \QuickQuote \Roles\ManageRoles.aspx.cs:31
System.Web.UI.WebControls.Button .OnClick(EventArgs e)+9819334
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler .RaisePostBackEvent(String eventArgument)+12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35
System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1639
我是假的经理对象没有像我期望的那样工作,并意识到这就是我被困住的地方。如何创建一个有效的扩展IdentityRole对象,我可以使用RoleExists方法?
I'm writing a new application and when I created the project initially using the template, it included the default user management features provided by ASP.NET. I'm not very familiar with this and have struggled to find documentation that wasn't part of an MVC application tutorial.
I've used a few tutorials and have an extended ApplicationRole class.
public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public string Description { get; set; } }
In the tutorials and template there is an IdentityConfig.cs file that has various classes including one for the ApplicationUserManager and I created an ApplicationRoleManager class using the code below:
public class ApplicationRoleManager : RoleManager<ApplicationRole> { public ApplicationRoleManager( IRoleStore<ApplicationRole, string> roleStore) : base(roleStore) { } public static ApplicationRoleManager Create( IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { return new ApplicationRoleManager( new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>())); } }
The problem comes with the first step: creating a new role. I'm trying to check if a role exists before I create it and thought the code (in the .aspx.cs file) below would work however I keep getting exceptions (pasted below the code)
protected void CreateRoleButton_Click(object sender, EventArgs e) { string newRoleName = RoleName.Text.Trim(); var manager = Context.GetOwinContext().GetUserManager<ApplicationRoleManager>(); if(!manager.RoleExists(newRoleName)) { var role = new ApplicationRole(newRoleName); manager.Create(role); } RoleName.Text = string.Empty; }
Exception:
Server Error in '/' Application. Value cannot be null. Parameter name: manager Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: manager Source Error: Line 29: Line 30: Line 31: if(manager.RoleExists(newRoleName)) Line 32: { Line 33: Source File: C:\Users\Administrator\Source\Repos\QuickQuote\QuickQuote\QuickQuote\Roles\ManageRoles.aspx.cs Line: 31 Stack Trace: [ArgumentNullException: Value cannot be null. Parameter name: manager] Microsoft.AspNet.Identity.RoleManagerExtensions.RoleExists(RoleManager`2 manager, String roleName) +155 QuickQuote.Roles.ManageRoles.CreateRoleButton_Click(Object sender, EventArgs e) in C:\Users\Administrator\Source\Repos\QuickQuote\QuickQuote\QuickQuote\Roles\ManageRoles.aspx.cs:31 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9819334 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
I'm assuming that the manager object isn't working as I'd expect and realise this is where I'm stuck. How do I create a valid extended IdentityRole object that I can use the RoleExists method on?
这篇关于将用户角色添加到Visual Studio Web应用程序(Web窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!