C# 在ASP.Net MVC Core 2.0中设置登录url
由于我在与身份验证相关的东西方面做得不够好,我想将自定义身份验证移植到“.NETCore2.0”,我需要帮助。有几个类似的问题,但我的有点不同。用户可以轻松登录和注销项目,只需设置用户未登录时的登录URL,并应重定向到登录页面 我已经检查了(、或其他几页,但它们大多过时了-与旧版本相关-或者它们不适合我的情况) My Startup.cs:C# 在ASP.Net MVC Core 2.0中设置登录url,c#,authentication,url,asp.net-core-mvc,asp.net-core-2.0,C#,Authentication,Url,Asp.net Core Mvc,Asp.net Core 2.0,由于我在与身份验证相关的东西方面做得不够好,我想将自定义身份验证移植到“.NETCore2.0”,我需要帮助。有几个类似的问题,但我的有点不同。用户可以轻松登录和注销项目,只需设置用户未登录时的登录URL,并应重定向到登录页面 我已经检查了(、或其他几页,但它们大多过时了-与旧版本相关-或者它们不适合我的情况) My Startup.cs: // This method gets called by the runtime. Use this method to add services to
// 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?}");
});
}
//此方法由运行时调用。使用此方法向容器中添加服务。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
var builder=services.AddMvc(选项=>{
Insert(0,new Olive.Mvc.OliveBinderProvider());
})
.AddJsonOptions(选项=>
{
options.SerializerSettings.ContractResolver=new Newtonsoft.Json.Serialization.DefaultContractResolver();
})
.ConfigureApplicationPartManager(管理器=>
{
var oldMetadataReferenceFeatureProvider=manager.FeatureProviders.First(f=>f是MetadataReferenceFeatureProvider);
manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
添加(新引用MetadataReferenceFeatureProvider());
}); ;
services.AddSingleton();
services.AddSingleton();
服务。附加性();
服务.AddAuthentication(IdentityConstants.ApplicationScheme)
.AddCookie(opt=>opt.LoginPath=“/login”);
//添加IDistributedCache的默认内存内实现。
AddDistributedMemoryCache();
services.AddSession();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseAuthentication();
app.UseStaticFiles();
app.UseSession();
app.UseMvc(路由=>
{
//routes.MapRoute(
//名称:“默认”,
//模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
Asasp.net core 2.0
已将其更改为使用ConfigureApplicationOkie
方法。有关将标识迁移到Core 2.0的详细信息。是否向要保护的控制器和操作方法添加了Authorize属性?是的。问题是用户被重定向到错误的URL(“/Account/Login”而不是“/Login”)。帮忙?