ASP.NET MVC5-将用户保留在Oracle数据库中 [英] ASP.NET MVC5 - Keeping Users in Oracle Database

查看:119
本文介绍了ASP.NET MVC5-将用户保留在Oracle数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦创建一个 ASP.NET MVC5 项目(目标框架为 .NET 4.5.1 ,并且身份验证类型为单个用户帐户), ),那么配置项目以使用户,声明,角色等保留在Oracle 12c数据库中的最优雅的方法是什么?我的意思是,如何在不破坏自动生成的MVC5项目结构的情况下将授权/身份验证数据保留在Oracle中.

Once creating a ASP.NET MVC5 project (with target framework is .NET 4.5.1 and the authentication type is Individual User Account), so what is the most elegant way to configure the project so that it keeps the user, claims, roles etc. in an Oracle 12c database? I mean, how can I keep the authorization/authentication data in Oracle without deforming the automatically-generated MVC5 project structure.

我猜想更改<defaultConnection>标记是不够的,应该有另一个Oracle实现来替换Microsoft.AspNet.Identity.EntityFramework.从头开始逐步收集答案,这将非常有帮助,Visual Studio在模板项目的生成位置(即应将哪些引用添加到项目;应以哪种方式添加Web.config文件)应该将Oracle表严格命名为AspNetUsers,AspNetClaims等,还是可以将已经存在的表名注入代码端?)

I guess changing the <defaultConnection> tag is not enough and there should be another Oracle implementation to replace Microsoft.AspNet.Identity.EntityFramework. It would be very helpful to gather an answer telling what to do step by step right from the start, where the template project is generated by VisualStudio (i.e. which references should be added to the project; in which way the Web.config file should be arranged? Should the Oracle tables be named strictly as AspNetUsers, AspNetClaims etc. or can I inject the already existing table names into the code side?)

注意: Devart的针对Oracle的dotConnect不在我的范围内,因为它不是免费的产品.我使用Oracle ManagedDataAccess进行数据库访问并使用Entity Framework,但是将 ASP.NET Identity 2 + EntityFramework6 逻辑与Oracle数据库一起使用(无需深深更改经典MVC5项目的结构)令我沮丧

Note: Devart's dotConnect for Oracle is out of my scope as it is not a free product. I use Oracle ManagedDataAccess for database access and to make use of Entity Framework, but using the ASP.NET Identity 2 + EntityFramework6 logic with Oracle database (without deeply changing the structure of the classical MVC5 project) has been frustrating to me.

推荐答案

这可能为您带来一些延迟,但如果有人遇到同样的问题,我会保留. 因此,我最终设法使Identity 2.0和Oracle一起工作.如果您不想对默认的IdentityUser进行任何更改(例如,您可以使用char ID而不是int或long),可以执行以下步骤,而仅希望现有Oracle模式上的表.

This may arrive a bit late for you but I'll leave it in case anyone else runs into the same problem. So I finally managed to make Identity 2.0 and Oracle work together. The following steps work if you don't want to make any changes to the default IdentityUser (ex. if you're ok with having a char ID instead of int or long) and just want the tables on your existing Oracle schema.

  1. 在Oracle上创建身份表.您可以根据需要更改表名称,只需确保包括必要的列即可使用Identity.您还可以在应用程序上添加任何可能需要的额外列(脚本最初在 Devart ,我将其复制到要点中,以防URL中断):

  1. Create Identity tables on Oracle. You can change the table names if you want to, just make sure to include the necessary columns for Identity to work with it. You can also add any extra columns you may need on your application (script originally found on Devart, I copied it to a gist in case URL breaks):

要点此处

如果使用的是EDMX文件,则需要添加新的连接字符串,因为自动生成的连接字符串将无法工作,因此需要标准的连接字符串.尝试遵循以下模板:

If you're using an EDMX file, you need to add a new connection string cause the one that gets generated automatically won't work, you need a standard connection string. Try following this template:

<add name="IdentityContext" connectionString="Data Source=localhost:1521/xe;PASSWORD=password;USER ID=username;" providerName="Oracle.ManagedDataAccess.Client" />

告诉您的ApplicationDbContext使用新的connectionString

Tell your ApplicationDbContext to use your new connectionString

public ApplicationDbContext()
    : base("IdentityContext", throwIfV1Schema: false)
{
}

  • 告诉身份以使用您现有的架构和表.在IdentityModels.cs中找到的ApplicationDbContext定义中添加此方法:

  • Tell Identity to use your existing schema and tables. Add this method inside the ApplicationDbContext definition found in IdentityModels.cs:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); // MUST go first.
    
        modelBuilder.HasDefaultSchema("YOUR_SCHEMA"); // Use uppercase!
    
        modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins");
    }
    

  • 重建并仅此而已!

  • Rebuild and thats it!

    让我知道它是否对您有用!

    Let me know if it works for you!

    这篇关于ASP.NET MVC5-将用户保留在Oracle数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆