2014-02-16 39 views
7
  1. 如何設置ASP.Net身份使用SQL Server數據庫而不是LocalDB?Web API和ASP.Net身份與SQL Server而不是LocalDB

  2. 如何自定義使用自定義數據層的ASP.Net標識?

中的Web API .NET 4.5

Web.config中的變化使用Microsoft.AspNet.Identity.Core 1.0.0和6.0.0的EntityFramework:

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

錯誤調用API時/帳戶/註冊

{"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. 
+0

只需更改您的數據庫連接字符串。 –

+0

@Brendan我做到了,但仍期待着LocalDB。 – Babak

+0

請發佈您的web.config相關部分。 –

回答

6

如果您尚未更改MVC 5 Internet模板使用的基本配置,則ASP.NET Identity的連接字符串的名稱爲「DefaultConnection」。嘗試使用此名稱創建連接字符串,並將其配置爲指向sql server。

如果您使用了Web API模板,那麼默認設置爲「No Authentication」,如下圖所示。

Web API Template without Authentication

如果選擇「無驗證」,那麼未安裝ASP.NET身份相應的組件,則不會創建一個帳戶控制器和web.config中不會更新與連接字符串。請注意,MVC仍然包含在模板中。

如果你想使用ASP.NET身份選擇「更改身份驗證」,然後選擇「個人帳戶」

Web API Template with Authentication

現在你擁有所有必需的組件,ASP.NET標識在啓動有線配置,並且默認連接字符串位於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> 

請注意,您也爲實體框架進行了配置,並且具有正確的程序集。默認情況下,ASP.NET標識首先使用EF代碼作爲數據存儲。默認設置爲使用本地數據庫。只需將此連接字符串更改爲使用SQL Server數據庫即可。

如果要將基礎數據存儲更改爲SQL Server以外的其他存儲,則有a tutorial you can follow here。這個特定的教程展示瞭如何將其更改爲MySql。

+0

如何將「DefaultConnection」更改爲其他名稱? – Babak

+1

查看更改連接字符串名稱的這些QA。 http://stackoverflow.com/questions/18297052/asp-net-identity-how-to-set-target-db http://stackoverflow.com/questions/19813374/asp-net-identity-in-mvc5-set -up連接字符串 –

相關問題