C# 使用 Oracle 数据库配置 ASP.NET MVC 4 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17915409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:47:56  来源:igfitidea点击:

Configure ASP.NET MVC 4 Application with Oracle Database

c#asp.net-mvc-4oracle11gr2

提问by Christian Mark

I am currently working on ASP.NET MVC 4 project with Oracle database. I have successfully add the connection string in my Web.config file like here:

我目前正在使用 Oracle 数据库开发 ASP.NET MVC 4 项目。我已经成功地在我的 Web.config 文件中添加了连接字符串,如下所示:

<add name="OracleDBConnString" connectionString="Provider=MSDAORA;Data Source=ISDQA;User ID=isd;Password=isdqa;" providerName="System.Data.OleDB" />

But when I create a new project, it has already have a built-in authentication classes. How can I modify these classes once and for all? I want to change the default ConnString.

但是当我创建一个新项目时,它已经有一个内置的身份验证类。如何一劳永逸地修改这些类?我想更改默认ConnString.

Here's my model:

这是我的模型:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("OracleDBConnString")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

public class LoginModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Here's my User controller:

这是我的用户控制器:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    //I WANT TO MODIFY THIS PART
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) 
    {

        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

UPDATE

更新

When I change the base("DefaultConnection")to base("OracleDBConnString"), I got this error:

当我改变了base("DefaultConnection")base("OracleDBConnString"),我得到这个错误:

Server Error in '/' Application.

A null was returned after calling the 'get_ProviderFactory' method on a store provider instance of type 'System.Data.OleDb.OleDbConnection'. The store provider might not be functioning correctly.

Note that I already added using System.Data.OleDb;on my usings.

请注意,我已经添加using System.Data.OleDb;了我的使用。

采纳答案by Piotr Stapp

Instead of creating special class you can setup in web.config which membership provider you use.

您可以在 web.config 中设置您使用的成员资格提供程序,而不是创建特殊类。

For Oracle there exist an OracleMembershipProviderclass, which you can find here. For MySQL you can use MySQLMembershipProvider. There is a nice article how you can setup it: How to Setup and Configure MySql Membership Provider

对于 Oracle,存在一个OracleMembershipProvider类,您可以在此处找到。对于 MySQL,您可以使用MySQLMembershipProvider. 有一篇很好的文章如何设置它:How to Setup and Configure MySql Membership Provider

Moreover you can use Entity Framework Code First Membership Providerand use proper provider (look on this article: Entity Framework Code-First support for Oracle, MySQL, PostgreSQL and SQLite). I did not try this solution, but it looks promising.

此外,您可以使用Entity Framework Code First Membership Provider并使用适当的提供程序(查看本文:Entity Framework Code-First support for Oracle、MySQL、PostgreSQL 和 SQLite)。我没有尝试这个解决方案,但它看起来很有希望。

If nothing above helps you can write your own Memebership provider, but I am sure that you can find something which already exists.

如果以上没有任何帮助,您可以编写自己的会员资格提供程序,但我相信您可以找到已经存在的内容。

To sum up: Instead of changing connection type, change membership provider which will be specific for your needs. Your code shouldn't notice change, because every membership provider inherits base MemebershipProivderclass

总结:与其更改连接类型,不如更改特定于您的需求的成员资格提供商。您的代码不应注意到更改,因为每个成员资格提供程序都继承基MemebershipProivder

回答by Atilla Ozgur

Asp.Net SimpleMembership and Asp.Net Membership are different beasts, I am afraid. Asp.Net MVC4 templates use SimpleMembership (WebSecurity.Login).

Asp.Net SimpleMembership 和 Asp.Net Membership 恐怕是不同的野兽。Asp.Net MVC4 模板使用 SimpleMembership (WebSecurity.Login)。

See my answer about MySql Membership provider usage in Asp.Net MVC4 here. Simply changing connection string or other things will not help you. You need to find an Oracle Asp.Net SimpleMembership or write your own.

请在此处查看我关于 Asp.Net MVC4 中 MySql Membership 提供程序使用情况的回答。简单地更改连接字符串或其他东西对您没有帮助。您需要找到 Oracle Asp.Net SimpleMembership 或自己编写。