5
votes

i want to create dropdown list with AspNetRoles . i use this code :

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>()));
    }
}

.

StartUP:

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 Config :

<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" />

Controller :

[AllowAnonymous]
public ActionResult Register()
{
    ViewBag.name = new SelectList(db.Roles, "RoleID", "RoleName");

    return View();
}

View :

<div class="form-group">
    <label>نوع عضویت</label>
    <div class="col-md-10">
        @Html.DropDownList("name", "--Select Name--")
    </div>
</div>

but when i run the project show me this error :

The following errors occurred while attempting to load the app. - The OwinStartupAttribute.FriendlyName value '' does not match the given value 'Identity_Work.IdentityConfig' in Assembly 'Identity_Work, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. - The given type or method 'Identity_Work.IdentityConfig' was not found. Try specifying the Assembly. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

. whats the problem ?

Edit

enter image description here

2

2 Answers

8
votes

Like the error message explains

The OwinStartupAttribute.FriendlyName value '' does not match the given value 'Identity_Work.IdentityConfig'

Follow the instruction of the error message

The given type or method 'Identity_Work.IdentityConfig' was not found. Try specifying the Assembly. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

First You should check Startup.cs to see if it has the correct reference to the class

[assembly: OwinStartup(typeof(Identity_Work.Startup))]

If it does then you need to remove owin:AppStartup in web.config if it exists and is not referencing the correct class

<add key="owin:AutomaticAppStartup" value="true" />

Otherwise you can update the web.config to let owin use

<add key="owin:AutomaticAppStartup" value="false" />
<add key="owin:AppStartup" value="Identity_Work.Startup" />
1
votes

I just specify the complete route and it works for me!

Project name: Users.Web
Folders: App_Start
Class name: IdentityConfig

<add key="owin:AppStartup" value="Users.Web.App_Start.IdentityConfig" />

enter image description here