I have a web application in Asp.net with Entity Framework 6 and a database. I have a problem connecting a user. Here is my code:
Web.config
<connectionStrings> <add name="DefaultConnection" connectionString="data source=MyServer\MyDataBase;initial catalog=Cliniciel_WebRV_Master;User Id=XXX;Password=XXXX" providerName="System.Data.SqlClient" /> <add name="Cliniciel_WebRV_MasterEntities" connectionString="metadata=res://*/Models.Entities.Cliniciel_WebRV_Master.csdl|res://*/Models.Entities.Cliniciel_WebRV_Master.ssdl|res://*/Models.Entities.Cliniciel_WebRV_Master.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer\MyDataBase;initial catalog=Cliniciel_WebRV_Master;user id=XXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> <add name="Cliniciel_WebRV_Entities" connectionString="metadata=res://*/Models.Entities.Cliniciel_WebRV_Entities.csdl|res://*/Models.Entities.Cliniciel_WebRV_Entities.ssdl|res://*/Models.Entities.Cliniciel_WebRV_Entities.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer\MyDataBase;initial catalog=Cliniciel_WebRV_DEV;user id=XXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> <add name="Cliniciel_WebRV_Oauth" connectionString="metadata=res://*/Models.Entities.Cliniciel_WebRV_Oauth.csdl|res://*/Models.Entities.Cliniciel_WebRV_Oauth.ssdl|res://*/Models.Entities.Cliniciel_WebRV_Oauth.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer\MyDataBase;initial catalog=Cliniciel_WebRV_Master;user id=XXX;password=XXXX;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
Here I use the connection string "Cliniciel_WebRV_Oauth" for my authentication authentication.
I configure my oauthToken at startup
Startup.cs
private void ConfigureOAuthTokenGeneration(IAppBuilder app) {
ApplicationDBContext.cs
using Microsoft.AspNet.Identity.EntityFramework; using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.SessionState; using WebRV.Models.Entities; namespace WebRV.Infrastructure { public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
CustomOAuthProvider.cs
using System; using System.Linq; using WebRV.Infrastructure; using Microsoft.Owin.Security; using Microsoft.Owin.Security.OAuth; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity.Owin; using System.Web.Mvc; using WebRV.Models.Entities; using System.Net; using System.Web.Http; namespace WebRV.Providers { public class CustomOAuthProvider : OAuthAuthorizationServerProvider { [ValidateAntiForgeryToken] public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { context.Validated(); return Task.FromResult<object>(null); } public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); } else {
Here is the error I received:
The ApplicationUser object type is not part of the model for the current context.
here's the trace:
Ligne 32: var userManager = context.OwinContext.GetUserManager (); Lin 33: Ligne 34: User ApplicationUser = wait userManager.FindAsync (context.UserName, context.Password); Ligne 35: Ligne 36: if (user == null)
Source Fichier: c: \ Users \ aboucher \ Desktop \ WebRV-2016-05-12 - Copie \ Cliniciel_WebRV \ WebRV \ WebRV \ Providers \ CustomOAuthProvider.cs
Ligne: 34
Trace:
[InvalidOperationException: Le type d'entitΓ© ApplicationUser ne fait pas partie du modelle pour le contexte actuel.]
System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType (type entityType) +4479799
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType (type entityType) +37
System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext () +16 System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() +39 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1 source, expression 1 predicate, CancellationToken cancellationToken) +154 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1 source, expression 1 predicate) +163
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +807 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1 predicate) +163
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +807 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1 predicate) +163
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +807 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult () +28 Microsoft.AspNet.Identity.CultureAwaiter 1.GetResult() +123 Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +601
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() +123 Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +601
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() +123 Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +601
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult () +28 WebRV.Providers.d__0.MoveNext () in c: \ Users \ Web \ Voucher \ User \ ab \ 2016-05-12 - COPIE \ Cliniciel_WebRV \ WebRV \ WebRV \ Providers \ CustomOAuthProvider.cs: 34 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult () +26 Microsoft.Owin.Security.OAuth.d__3f.MoveNextRate.aime.er .ThrowForNonSuccess (Task Task) +92
1.GetResult() +28 Microsoft.Owin.Security.OAuth.<InvokeTokenEndpointAsync>d__22.MoveNext() +2336 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.OAuth.<InvokeAsync>d__0.MoveNext() +1733 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() +28 Microsoft.Owin.Security.OAuth.<InvokeTokenEndpointAsync>d__22.MoveNext() +2336 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.OAuth.<InvokeAsync>d__0.MoveNext() +1733 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() +28 Microsoft.Owin.Security.OAuth.<InvokeTokenEndpointAsync>d__22.MoveNext() +2336 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.OAuth.<InvokeAsync>d__0.MoveNext() +1733 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult () +28 Microsoft.Owin.Security.Infrastructure.d__0.MoveNextRenter.er64.6464 TaskAwaiter.ThrowForNonSuccess (Task task) +92
System.Runtime .ThrowForNonSuccess (Task Task) +92
System.Runtime .ThrowForNonSuccess (Task Task) +92
System.Runtime .TaskAwaiter.ThrowForNonSuccess (Task Task) +92
System.Runtime .ExceptionDispatchInfo.Throw () +26 Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow () +33 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End (IAsyncResult ar) +150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork (IAsyncResult ar) +42
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +380 System.Web.HttpApplication.ExecuteStep (step IExecutionStep, Boolean and completed synchronously) +155
alexandre
source share