c# - 在 ASP.Net MVC Core 2.0 中设置登录 url

标签 c# authentication url asp.net-core-mvc asp.net-core-2.0

由于我在身份验证相关方面不够好,我想将自定义身份验证移植到“.Net Core 2.0”,我需要帮助。那里有几个类似的问题,但我的有点不同。用户可以轻松登录和退出项目,只需要设置用户未登录时的登录URL,并重定向到登录页面。

我已经检查过( thisthis 或其他几个页面,但它们大多已过时 - 与旧版本相关 - 或者它们不适合我的情况) 我的 Startup.cs:

// 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 https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvc(options => {
            options.ModelBinderProviders.Insert(0, new Olive.Mvc.OliveBinderProvider());
        })
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        })
        .ConfigureApplicationPartManager(manager =>
        {
            var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
            manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
            manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
        }); ;

        services.AddSingleton<IUserStore<User>, UserStore>();
        services.AddSingleton<IRoleStore<string>, RoleStore>();
        services.AddIdentity<User, string>();
        services.AddAuthentication(IdentityConstants.ApplicationScheme)
            .AddCookie(opt => opt.LoginPath = "/login");

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.AddSession();
    }

    // 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();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseAuthentication();

        app.UseStaticFiles();

        app.UseSession();

        app.UseMvc(routes =>
        {
            //routes.MapRoute(
            //    name: "default",
            //    template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

最佳答案

作为shown here . asp.net core 2.0改成使用ConfigureApplicationCookie方法。有关将身份迁移到 Core 2.0 的更多信息 here .

关于c# - 在 ASP.Net MVC Core 2.0 中设置登录 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217948/

相关文章:

java - JEdi​​torPane 的奇怪行为

ios - 隐藏从API发送的重置密码电子邮件上的Parse.com URL

c# - 集合中集合的 Where 子句

c# - 在 JavaScript 中访问 C# 变量

javascript - 在 React Native 中如何获取带认证的网络图片?

swift - 使用 Alamofire 进行身份验证的请求

ruby-on-rails - Rails Routes - 斜杠字符与哈希字符

c# - 在datagridview c#中更改搜索关键字的颜色

c# - 带有 IList<SelectedItemList> 问题的 LINQ

authentication - Gitlab:无法为电子邮件设置中继地址(state=SSLv2/v3 read server hello A: unknown protocol”)