Ich versuche, Authentifizierung hinzuzufügen, um Authentifizierung zu einer vorhandenen MVC 5-Anwendung hinzuzufügen, die ich von einem leeren Projekt gestartet habe. Ich habe ein neues WebAPI-Projekt mit einzelnen Benutzerkonten gestartet, damit ich sehen konnte, wie es konfiguriert wurde. Ich habe den Code kopiert, der mit der Authentifizierung zu tun hat, und die Namespaces und Klassennamen überarbeitet. Im folgenden Code gibt die erste Zeile var identityContext = context.Get<IdentityDbContext>()
null zurück und bewirkt, dass die zweite Zeile var userStore = new UserStore<AdminAppUser>(identityContext)
aufgrund eines NULL-Parameters einen Fehler auslöst.IOwinContext.Get <DbContext>() gibt null zurück
Ich habe wahrscheinlich nicht genug Code enthalten, da ich sehr neu in der MVC-Authentifizierung bin und ein schlechtes Verständnis davon habe, wie alle Teile zusammenpassen. Wenn ich mehr Code hinzufügen muss, lassen Sie mich bitte wissen, welche Teile nützlich wären. Vielen Dank!
public static AdminAppUserManager Create(IdentityFactoryOptions<AdminAppUserManager> options, IOwinContext context)
{
var identityContext = context.Get<IdentityDbContext>();
var userStore = new UserStore<AdminAppUser>(identityContext);
var manager = new AdminAppUserManager(userStore);
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<AdminAppUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<AdminAppUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
EDIT:
startup.auth.cs
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(AdminAppIdentityDbContext.Create);
app.CreatePerOwinContext<AdminAppUserManager>(AdminAppUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
//{
// ClientId = "",
// ClientSecret = ""
//});
}
}
startup.cs:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
Edit 2:
Ich habe meinen Startup-Klassencode in meiner Bearbeitung hinzugefügt. Dieser Anruf ist schon da. –
Welchen Typ gibt AdminAppIdentityDbContext.Create zurück? – DavidS
es gibt 'neues AdminAppIdentityDbContext();' –