gpt4 book ai didi

asp.net-identity - 使用 SQL Server 而不是 LocalDB 的 Web API 和 ASP.Net 标识

转载 作者:行者123 更新时间:2023-12-03 15:46:06 35 4
gpt4 key购买 nike

  • 如何设置 ASP.Net Identity 以使用 SQL Server 数据库而不是 LocalDB?
  • 如何自定义 ASP.Net Identity 以使用自定义数据层?

  • 在 Web API .Net 4.5 中使用 Microsoft.AspNet.Identity.Core 1.0.0 和 EntityFramework 6.0.0

    Web.config 更改:
    <connectionStrings>
    <add name="myAppDB"
    connectionString="data source=.;Initial Catalog=mydb1;Integrated Security=SSPI;"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    调用 api/account/register 时出错
    {"Message":"An error has occurred.","ExceptionMessage":"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.","ExceptionType":"System.Data.Entity.Core.ProviderIncompatibleException","StackTrace":"   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerSeres.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The provider did not return a ProviderManifestToken string.","ExceptionType":"System.Data.Entity.Core.ProviderIncompatibleException","StackTrace":" at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
    at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
    )","ExceptionType":"System.Data.SqlClient.SqlException","StackTrace":" .....

    和 Windows 应用程序事件日志
    Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.

    最佳答案

    如果您尚未更改 MVC 5 Internet 模板使用的基本配置,则 ASP.NET 标识的连接字符串的名称为“DefaultConnection”。尝试使用此名称创建连接字符串并将其配置为指向 sql server。

    如果您使用 Web API 模板,则默认设置为“无身份验证”,如下图所示。

    Web API Template without Authentication

    如果选择“无身份验证”,则不会安装 ASP.NET 标识的相应程序集,不会创建帐户 Controller ,并且不会使用连接字符串更新 web.config。请注意,MVC 仍包含在模板中。

    如果要使用 ASP.NET Identity,请选择“更改身份验证”并选择“个人帐户”

    Web API Template with Authentication

    现在您拥有了所有必需的程序集,ASP.NET Identity 连接在启动配置中,默认连接字符串位于您的 web.config 中。

      <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source= (LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-TestWebApiTemplateWithAuth20140218082219.mdf;Initial Catalog=aspnet-TestWebApiTemplateWithAuth- 20140218082219;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    请注意,您还针对 Entity Framework 进行了配置,并为此拥有正确的程序集。默认情况下,ASP.NET Identity 对数据存储使用 EF 代码优先。默认设置为使用本地数据库。只需更改此连接字符串即可使用 SQL Server 数据库。

    如果您想将基础数据存储更改为 SQL Server 以外的其他内容,则可以使用 a tutorial you can follow here .这个特定的教程展示了如何将其更改为 MySql。

    关于asp.net-identity - 使用 SQL Server 而不是 LocalDB 的 Web API 和 ASP.Net 标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21816388/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com