加入收藏 | 设为首页 | 会员中心 | 我要投稿 甘南站长网 (https://www.0941zz.com/)- 科技、行业物联网、开发、云计算、云管理!
当前位置: 首页 > 编程开发 > asp.Net > 正文

User.Identity始终为null

发布时间:2023-02-17 09:05:25 所属栏目:asp.Net 来源:互联网
导读:如何解决User.Identity始终为null? 我需要检查用户是否已登录。在View中,我正在像这样检查它; @if (User.Identity.IsAuthenticated) { //links... } else { //links... } 尽管我在登录功能中登录了SignInAsync,但始终返回false,并且身份为空。我试图更
  如何解决User.Identity始终为null?
  我需要检查用户是否已登录。在View中,我正在像这样检查它;
 
                  @if (User.Identity.IsAuthenticated)
                  {
                      //links...
                  }
                  else
                  {
                      //links...
                  }
  尽管我在登录功能中登录了SignInAsync,但始终返回false,并且身份为空。我试图更改usings的configure方法的顺序,但是没有用。 这是我的启动和登录功能。
 
   public void ConfigureServices(IServiceCollection services)
      {
          var key = Encoding.ASCII.GetBytes(Configuration.GetSection("Appsettings:Secret").Value);
          services.AddDbContext<BiHaberContext>();
          services.AddIdentity<ApplicationUser,ApplicationRole>()
              .AddEntityFrameworkStores<BiHaberContext>()
              .AddDefaultTokenProviders();
          services.Configure<IdentityOptions>(options =>
          {
              options.Password.requiredigit = false;
              options.Password.requiredLength = 3;
              options.Password.RequireLowercase = false;
              options.Password.RequireUppercase = false;
              options.Password.RequireNonAlphanumeric = false;
              options.Lockout.MaxFailedAccessAttempts = 3;
              options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
              options.User.RequireUniqueEmail = false;
              options.SignIn.RequireConfirmedEmail = false;
              options.SignIn.RequireConfirmedPhoneNumber = false;
              options.SignIn.RequireConfirmedAccount = false;
          });
          services.AddAutoMapper(typeof(Startup));
          services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
          {
              options.TokenValidationParameters = new TokenValidationParameters
              {
                  ValidateIssuerSigningKey = true,IssuerSigningKey = new SymmetricSecurityKey(key),ValidateIssuer = false,ValidateAudience = false
              };
          });
          services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
              .AddCookie(options =>
              {
                  //options.Cookie.HttpOnly = true;
                  options.ExpireTimeSpan = TimeSpan.FromDays(30);
                  options.LoginPath = "/Login";
                  options.AccessDeniedpath = "/Identity/Account/AccessDenied";
                  options.SlidingExpiration = true;
              });
          services.AddAuthentication();
          services.AddAuthorization();
          services.AddControllersWithViews();
          services.AddScoped<ISemesterService,SemesterManager>();
          services.AddScoped<IDepartmentService,DepartmentManager>();
          services.AddScoped<ICourseService,CourseManager>();
          services.AddScoped<IAnnouncementService,AnnouncementManager>();
          services.AddCors();
          services.AddResponseCaching();
          services.AddMemoryCache();
      }
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
              app.UseHsts();
          }
          app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyHeader());
          app.UseResponseCaching();
          app.UseHttpsRedirection();
          app.UseStaticFiles();
          app.UseRouting();
          app.UseAuthentication();
          app.UseAuthorization();
          app.UseCookiePolicy();
          app.UseEndpoints(endpoints =>

(编辑:甘南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读