2016-04-13 15 views
5

アプリケーションに認証を追加しようとしていますが、エンティティフレームワークを実行していますが、今はユーザーを認証したいのですが、構成で構成する際に多くの問題が発生していますコンストラクタ。彼らはもはや私がASP.NETコア1.0で認証を構成する方法

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

をすればそれは私が明示的に型引数を指定する必要があることを私に伝えますが、これはチュートリアルにあるものであるように動作するコードを提供する多くのチュートリアルでは例えば

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

私はこれを行うための正しい方法が何であるかを理解することに苦労しています、私がやりたいすべては彼/彼女がログインするときに、ユーザーを認証である。

ここに私のプロジェクトです。 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" 
    }, 

と私の設定:

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); 
} 

私はアイデアが不足しているので、私は同じ結果を持つ他のいくつかのサイトを試してみましたか?

+0

ASP.NET Core 1.0はまだ終了していません。このチュートリアルは、ほとんど1年前の5月からのものです。それ以来多くのことが変わってきました。できるだけ早くRCバージョンからのあなたを助けるために最近のチュートリアルを見つけることを試みなさい。 –

+0

見つけられませんでした、私に助けてくれるリンクがありますか?多分私はそれを間違って探しています。ありがとうございます –

+0

パッケージのバージョンを混用しないでください。問題を引き起こしています。あなたのproject.jsonはrc1-final、beta4、beta5、beta7、beta8の混沌とし​​た組み合わせです。 – Tseng

答えて

9

あなたはRC1に二つの方法でIDを設定できます。

1 - あなたはアイデンティティに追加されている

例:

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 - 利用IdentityOptions

例:

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); 
      } 
     }; 
    }); 
} 

詳細情報: ASP.NET Authentication Doc

関連する問題