简体   繁体   English

麻烦授权asp.net核心

[英]trouble authorize asp.net core

Plz, help in my trouble. 请帮我解决麻烦。

I created new application in MS VS 2017. Type ASP.NET Core 2 MVC 我在MS VS 2017中创建了新应用程序。类型ASP.NET Core 2 MVC

I can't find solution for my problem with authorize. 我找不到授权问题的解决方案。

  1. I added to Startup 我添加到启动

      public void ConfigureServices(IServiceCollection services) { services.AddScoped<USDb, USDb>(); services.Scan(scan => scan.FromAssemblyOf<USDb>() .AddClasses() .AsImplementedInterfaces()); InjectionContainer = services.BuildServiceProvider(); services.AddMvc(); //Auth services.AddAuthentication(o => { o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o => { o.LoginPath = new PathString("/Account/Login"); o.ReturnUrlParameter = "RedirectUrl"; o.AccessDeniedPath = new PathString("/Account/AccessDenied"); o.LogoutPath = new PathString("/Account/Logout"); o.ExpireTimeSpan = TimeSpan.FromDays(1); o.SlidingExpiration = true; o.Cookie.SameSite = SameSiteMode.Strict; o.Cookie.Name = ".USAUTH"; }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); app.UseAuthentication(); } 
  2. My AccountController method 我的AccountController方法

      [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginModel model, string redirectUrl = null) { if (ModelState.IsValid) { await Authenticate("login"); return Redirect(redirectUrl); } return View(); } private async Task Authenticate(string userName) { 

    var claims = new List{ new Claim(ClaimsIdentity.DefaultNameClaimType, userName) }; var Claims = new List {new Claim(ClaimsIdentity.DefaultNameClaimType,userName)}; ClaimsIdentity id = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); ClaimsIdentity id = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);

      await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)); } 

When I try to go Home/Index application redirect me to account - it's very good. 当我尝试转到“首页/索引”应用程序时,将我重定向到帐户-很好。 But after I entered login data and submited it to login method application redirect me to login page again... and again.... etc But cookies appeared, but application not created HttpContext.User.Identity and still redirecting me to login page.... And I don't know what todo(((( please help(( I lose the hope( 但是,当我输入登录数据并将其提交到登录方法之后,应用程序将我再次重定向到登录页面……一再……。等等但是出现了cookie,但是应用程序未创建HttpContext.User.Identity,仍然将我重定向到登录页面。 ...而且我不知道该怎么办(((((请帮助((我失去了希望(

You must call app.UseAuthentication(); 您必须调用app.UseAuthentication(); before app.UseMvc(...); app.UseMvc(...);之前app.UseMvc(...); .

Banal error but I had the same problem a while ago after upgrading to asp.net core 2.0. 普通错误,但升级到asp.net core 2.0后不久,我遇到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM