Asp.net mvc 如何将IdentityServer v3/v4集成到MVC中以管理用户角色和声明?
在ASP.NET2005(v2时间框架)中,有一个基于web的工具称为ASP.NET网站管理工具,人们可以使用该工具编辑用户并管理ASP.NET成员数据库。这一有用的工具于2012年被删除,至今仍无人使用 已编辑-要将自定义角色集成到我的MVC应用程序中,正确的版本不是服务器,需要使用IdentityManager 编译解决方案。在Web.config中,将修改为工作SQL数据库。在我的例子中,我已经有一些aspIdentity表,因此必须删除它们 实体可以创建新的实体。现在,此identity manager代码应该运行并用于创建用户、设置角色和声明以及保存到表中 现在的目标是匹配数据库表和身份验证方案,以便其他一些新的MVC项目在这里查找其角色。这个 目前,IdentityManager软件将是设置角色的实用工具 在MVC应用程序中,转到工具NuGet,查找“identitymanager”,应该有3个测试版文件。获取identitymanager和aspIdentity。 项目还需要Owin(但我已经安装了它)。修改Startup.cs:Asp.net mvc 如何将IdentityServer v3/v4集成到MVC中以管理用户角色和声明?,asp.net-mvc,identityserver3,Asp.net Mvc,Identityserver3,在ASP.NET2005(v2时间框架)中,有一个基于web的工具称为ASP.NET网站管理工具,人们可以使用该工具编辑用户并管理ASP.NET成员数据库。这一有用的工具于2012年被删除,至今仍无人使用 已编辑-要将自定义角色集成到我的MVC应用程序中,正确的版本不是服务器,需要使用IdentityManager 编译解决方案。在Web.config中,将修改为工作SQL数据库。在我的例子中,我已经有一些aspIdentity表,因此必须删除它们 实体可以创建新的实体。现在,此identi
Public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.Map("/idm", idm =>
{
var factory = new IdentityManagerServiceFactory();
factory.IdentityManagerService = new Registration<IIdentityManagerService, ApplicationIdentityManagerService>();
factory.Register(new IdentityManager.Configuration.Registration<ApplicationUserManager>());
factory.Register(new IdentityManager.Configuration.Registration<ApplicationUserStore>());
factory.Register(new IdentityManager.Configuration.Registration<ApplicationDbContext>());
factory.Register(new IdentityManager.Configuration.Registration<ApplicationRoleManager>());
factory.Register(new IdentityManager.Configuration.Registration<ApplicationRoleStore>());
idm.UseIdentityManager(new IdentityManagerOptions
{
Factory = factory
});
});
}
}
这个角色是由实用程序创建的,存储在SQL中,然后我的MVC希望看到、使用、获取或应用这个角色,并且只允许我的用户帐户
授权的
现在,它将不会授权,并将浏览器发送回登录,因为授权失败,必须假定其为
如此接近,但什么能让它不起作用呢?Identity Server(OpenID连接提供商)和Identity Manager(您追求的身份管理工具)在2015年的某个时候放弃了Thinktecture前缀。因此,您可能正在使用过时的nuget软件包
此外,Identity Server 4使用.NET核心,Identity Server 3和Identity Manager使用.NET框架
如果您正在寻找有关Identity Manager入门的最新指南,我在今年早些时候发布的博客上有一个演练:谢谢您的解释,这似乎正是问题之一。我无法让你在那里的链接起作用。一旦我意识到这是需要的身份“经理”,我找到了不同的文档和视频,现在正在尝试;但请期待您的演练also@BradRogers这些链接对我来说似乎工作得很好,除非您引用的是链接中不工作的示例代码。还感谢斯科特·布雷迪的指导!是的,链接现在起作用了,我应该提到这一点。我似乎很难理解HTTPS。VisualStudio15使用IIS Express?Windows7有IIS管理器,这是相同的吗?我制作了SSL证书,将我的文字项目文件夹路径添加到IIS mgr,出现错误。我被卡住了,因为现在将我的网站设置为httpS会返回空白,没有网页。好的,这是你的部署问题。同时切换回IIS Express,让我们一次解决一个问题。您可以通过双击属性、转到Web并将下拉框设置为IIS Express来执行此操作。如果您现在遇到身份验证/授权问题,请查看ConfigureAuth方法。或者与默认的ASP.NET MVC with individual accounts模板相同?我认为ConfigureAuth是默认的。将其添加到上面的原始文件中。
public class ApplicationUserStore : UserStore<ApplicationUser>
{
public ApplicationUserStore(ApplicationDbContext ctx)
: base(ctx)
{
}
}
// public class ApplicationRole :
public class ApplicationRoleStore : RoleStore<IdentityRole>
{
public ApplicationRoleStore(ApplicationDbContext ctx)
: base(ctx)
{
}
}
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(ApplicationRoleStore roleStore)
: base(roleStore)
{
}
}
public class ApplicationIdentityManagerService : AspNetIdentityManagerService<ApplicationUser, string, IdentityRole, string>
{
public ApplicationIdentityManagerService(ApplicationUserManager userMgr, ApplicationRoleManager roleMgr)
: base(userMgr, roleMgr)
{
}
}
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
// public ApplicationUserManager(IUserStore<ApplicationUser> store)
public ApplicationUserManager(ApplicationUserStore store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
// var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
//{
// ClientId = "",
// ClientSecret = ""
//});
}
[Authorize(Roles ="Finance")]