gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:38:13 25 4
gpt4 key购买 nike

由于我在身份验证相关方面不够好,我想将自定义身份验证移植到“.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com