2016-04-13 14 views
5

Ich versuche, Authentifizierung zu meiner Anwendung hinzufügen, ich habe Entity Framework ausgeführt, aber jetzt möchte ich den Benutzer authentifizieren, aber ich bin in vielen Problemen bei der Konfiguration in der Konfiguration Konstrukteur.Konfigurieren der Authentifizierung in ASP.NET Core 1.0

Zum Beispiel in vielen Tutorials sie Code, der nicht funktioniert wie länger, wenn ich

// Configure ASP.NET Identity to use our Identity-based application context 
    services.AddAuthentication() 
     .AddIdentity() 
     .AddEntityFrameworkStores() 
     .AddDefaultTokenProviders(); 

Es sagt mir, dass ich explizit die Typargumente angeben müssen, aber das ist, was im Tutorial ist ?

https://shellmonger.com/2015/05/29/asp-net-mvc5-identity-part-1-the-database/

ich eine harte Zeit habe zu verstehen, was der richtige Weg ist, dies zu tun, alles, was ich tun möchte, ist ein Benutzer authentifizieren, wenn er/sie sich anmeldet.

Hier ist mein Projekt. json

"dependencies": { 
    "EntityFramework.Commands": "7.0.0-rc1-final", 
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", 
    "EntityFramework.Core": "7.0.0-rc1-final", 
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final", 
    "Microsoft.AspNet.Identity": "3.0.0-rc1-final", 
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final", 
    "Microsoft.AspNet.Authentication": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Authorization": "1.0.0-rc1-final", 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta5", 
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4", 
    "Microsoft.Framework.Logging": "1.0.0-beta7", 
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final" 
    }, 

und meine Konfiguration:

public class Startup 
{ 
    public IConfigurationRoot Configuration { get; set; } 

    public Startup() 
    { 

     var builder = new ConfigurationBuilder() 
     .AddJsonFile("config.json") 
     .AddJsonFile($"config.json", optional: true); 
     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 
    // This method gets called by the runtime. Use this method to add services to the container. 
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddEntityFramework() 
     .AddSqlServer() 
     .AddDbContext<OrganizationsAppContext>(options => 
      options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 

     // Specify the configuration of our Application database context 
     services.Configure<Option>(options => 
     { 
      options.DefaultUserName = Configuration.Get("DefaultUser:Username"); 
      options.DefaultUserPassword = Configuration.Get("DefaultUSer:Password"); 
     }); 

     // Configure ASP.NET Identity to use our Identity-based application context 
     //services.AddAuthentication() 
     // .AddIdentity() 
     // .AddEntityFrameworkStores() 
     // .AddDefaultTokenProviders(); DOES NOT WORK! 

     services.AddMvc(); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app) 
    { 
     app.UseIISPlatformHandler(); 
     app.UseMvc(); 
     app.UseDefaultFiles(); 
     app.UseStaticFiles(); 
     app.UseIdentity(); 
    } 

    // Entry point for the application. 
    public static void Main(string[] args) => WebApplication.Run<Startup>(args); 
} 

Jede Hilfe würde geschätzt werden, ich habe keine Ideen mehr, ich habe mehrere andere Websites mit dem gleichen Ergebnis ausprobiert (hat sich diese Vorgehensweise geändert?).

+0

Beachten Sie, dass ASP.NET Core 1.0 noch nicht fertig ist. Das Tutorial scheint vom letzten Mai vor fast einem Jahr zu sein. Seitdem hat sich viel verändert. Versuchen Sie, eine neuere Anleitung zu finden, die Ihnen am besten hilft, vorzugsweise von den RC-Versionen, die jetzt verfügbar sind. –

+0

Haben Sie nicht gefunden, haben Sie einen Link, mit dem Sie mir helfen könnten ?, Vielleicht suche ich es falsch. danke –

+0

Bitte nicht mischen Paketversionen, es ruft nach Ärger. Ihre project.json ist eine chaotische Mischung aus rc1-final, beta4, beta5, beta7 und beta8. – Tseng

Antwort

9

Sie können Identität auf zwei Arten in RC1 konfigurieren:

1- Wenn Sie hinzufügen Identität

Beispiel:

services.AddIdentity<User, Role>(config => { 
    // Config here 
    config.User.RequireUniqueEmail = true; 
    config.Password = new PasswordOptions 
    { 
     RequireDigit = true, 
     RequireNonLetterOrDigit = false, 
     RequireUppercase = false, 
     RequireLowercase = true, 
     RequiredLength = 8, 
    }; 
}).AddEntityFrameworkStores<ApplicationContext, int>() 
    .AddDefaultTokenProviders(); 

2- Verwenden IdentityOptions:

Beispiel:

services.Configure<IdentityOptions>(options => 
    { 
     options.Password = new PasswordOptions 
     { 
      RequireDigit = true, 
      RequireNonLetterOrDigit = false, 
      RequireUppercase = false, 
      RequireLowercase = true, 
      RequiredLength = 8, 
     }; 
     options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents 
     { 
      OnRedirectToLogin = ctx => 
      { 
       ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 
       return Task.FromResult<object>(null); 
      } 
     }; 
    }); 
} 

Weitere Informationen: ASP.NET Authentication Doc

Verwandte Themen