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 => (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net – 为什么HttpContext.Current.User.Ide
- 在IIS上部署ASP.NET Core项目的图文方法
- ASP.NET web.config中数据库连接字符串connectio
- asp.net – 确定当前页面是否需要授权?
- Asp.net webForm设置允许表单提交Html的方法
- asp.net-core – .NET Core SDK安装程序无法在Wi
- asp.net-mvc – Nhibernate / MVC:在View中处理
- asp.net-core – 我为什么要选择带有.Net核心的A
- asp.net-mvc – 如何将MVC 5 IdentityModels.cs移
- asp.net – 为不同项目中的所有Web应用程序网页添
热点阅读