我想用AspNetRoles
创建下拉列表。我使用这段代码:
Idnetity Conf:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
}
}
启动:
using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using Identity_Work.Models;
namespace Identity_Work
{
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}
}
。 Web配置:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="owin:AppStartup" value="Identity_Work.IdentityConfig" />
控制器:
[AllowAnonymous]
public ActionResult Register()
{
ViewBag.name = new SelectList(db.Roles, "RoleID", "RoleName");
return View();
}
查看:
<div class="form-group">
<label>نوع عضویت</label>
<div class="col-md-10">
@Html.DropDownList("name", "--Select Name--")
</div>
</div>
但是当我运行项目时显示这个错误:
尝试加载应用时发生以下错误。 - OwinStartupAttribute.FriendlyName值&#39;&#39;与给定值不匹配&#39; Identity_Work.IdentityConfig&#39;在Assembly&#39; Identity_Work,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39;。 - 给定的类型或方法&#39; Identity_Work.IdentityConfig&#39;没找到。尝试指定程序集。 要禁用OWIN启动发现,请添加appSetting owin:AutomaticAppStartup,其值为&#34; false&#34;在你的web.config中。 要指定OWIN启动程序集,类或方法,请在web.config中添加appSetting owin:AppStartup以及完全限定的启动类或配置方法名称。
。有什么问题 ?
修改
答案 0 :(得分:7)
与错误消息解释一样
OwinStartupAttribute.FriendlyName值''与 给定值'Identity_Work.IdentityConfig'
按照错误消息
的说明进行操作找不到给定的类型或方法“Identity_Work.IdentityConfig”。 尝试指定程序集。要禁用OWIN启动发现,请添加 appSetting owin:AutomaticAppStartup,其值为“false” 你的web.config。指定OWIN启动程序集,类或 方法,添加appSetting owin:AppStartup与完全限定 web.config中的启动类或配置方法名称。
首先您应该检查Startup.cs
以查看它是否具有对该类的正确引用
[assembly: OwinStartup(typeof(Identity_Work.Startup))]
如果是,那么你需要删除web.config中的owin:AppStartup
(如果它存在且没有引用正确的类
<add key="owin:AutomaticAppStartup" value="true" />
否则你可以更新web.config以让owin使用
<add key="owin:AutomaticAppStartup" value="false" />
<add key="owin:AppStartup" value="Identity_Work.Startup" />
答案 1 :(得分:0)
我只是指定完整的路线,它对我有用!
Project name: Users.Web
Folders: App_Start
Class name: IdentityConfig
<add key="owin:AppStartup" value="Users.Web.App_Start.IdentityConfig" />