如何在ASP.Net Core Razor中使用cookie添加我自己的身份验证?

时间:2018-11-28 18:07:13

标签: c# asp.net cookies httpcontext

ich verzweifle mittlerweile schon seit fast einer Woche。 MVC-Modell Entschieden中的C#和Hab mich bei der Isch schreibe eine Webseite(Weil ich das nicht so ganz verstehe ...)。 Ich habe还可以欣赏WebanwendungAusgewählt(Visual Studio 2017)和Komme bis jetzt ganz gut klar。 Ich habe Klassen wie在常规WPF比赛中获得冠军。 我的问题:Ich schaffe es einfach nicht Cookie-bzw。 Session-Variablen zu speichern。饼干kann ich schon auslesen,ebenso,wie fast alles andere-nur schreiben geht nicht。 (Wennfüreuch nicht alles klar sein sollte:Ich bin noch亲戚Neuling,PHP Webseiten gebaut帽子中的der zuvor nur。)

================================================ =====

大家好, 我已经绝望了近一个星期。我目前正在用C#编写网站,并决定不使用MVC页面(因为我不太了解...)。 因此,我选择了一个简单的Web应用程序(Visual Studio 2017),到目前为止我对此还满意。我有像普通WPF应用程序中的类,到目前为止,我管理得很好。 我的问题:我只是无法保存Cookie或会话变量。我已经可以阅读cookie以及几乎所有其他内容了-只有写作是不可能的。 (如果不是所有事情都对您清楚:我仍然是一个相对较新的人,曾经用PHP构建网站。) 顺便说一句:对不起,我可能英语不好。

Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Webpage {
    public class Startup {
        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            services.Configure<CookiePolicyOptions>(options => {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddHttpContextAccessor();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }
            else {
                app.UseExceptionHandler("/Error/500");
                app.UseHsts();
            }

            app.UseStaticHttpContext();

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }
    }
    public static class StaticHttpContextExtensions {
        public static void AddHttpContextAccessor(this IServiceCollection services) {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

        public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app) {
            var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
            System.Web.HttpContext.Configure(httpContextAccessor);
            return app;
        }
    }
}

namespace System.Web {
    public static class HttpContext {
        private static IHttpContextAccessor _contextAccessor;

        public static Microsoft.AspNetCore.Http.HttpContext Current => _contextAccessor.HttpContext;

        internal static void Configure(IHttpContextAccessor contextAccessor) {
            _contextAccessor = contextAccessor;
        }
    }
}

HttpConnection.cs:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Webpage.Classes {
    public class HttpConnection {
        public static IPAddress GetUserIPAddress() {
            return System.Web.HttpContext.Current.Connection.RemoteIpAddress;
        }

        public static string addReplaceCookie(string cookieName, string cookieValue) {
            System.Web.HttpContext.Current.Response.Cookies.Append(cookieName, cookieValue, new CookieOptions {
                Expires = DateTime.Now.AddDays(1),
                Domain = null
            });
            return System.Web.HttpContext.Current.Request.Cookies[cookieName];
        }
    }
}

0 个答案:

没有答案