![](/img/trans.png)
[英]Difference between creating my asp.net MVC web application ,under the IIS Default web site, or create it as a new web site
[英]ASP.NET CORE 3.0 application to IIS under Default Web Site/MySite does not work properly
我的 Asp.Net Core 項目在默認網站之外正常運行,但它不能在 iis 上的默認網站下運行。 實際上,當我右鍵單擊“默認網站”下的應用程序文件夾並在 chrome 中瀏覽 mysite 時,它會顯示我的登錄屏幕和地址,如下所示: https : //testserver.abc.com/test_site或https://testserver.abc .com/test_site/account/login
在我輸入登錄信息后(我在本地開發機器上嘗試過的信息是真的),它無法重定向到 home/index 或其他鏈接,因為鏈接變成了https://testserver.abc.com/account/login 。 它正在從鏈接中刪除應用程序文件夾名稱。 我無法解決這個重定向問題。 任何解決方案的報價? 謝謝..
因此,登錄頁面后會出現 404 not found 錯誤。 但是如果我添加應用程序文件夾名稱來鏈接它工作正常,除非您單擊另一個鏈接:)
我的代碼部分:
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)
{
IdentityModelEventSource.ShowPII = true;
services.AddDbContext<LabStokContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().AddNToastNotifyNoty();
//Not: Dependency hizmetleri ayrı bir sınıfta çağırılıyor
services.ServisEkle();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.GetEncoding("UTF-8");
services.AddControllersWithViews().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin",
builder => builder.WithOrigins("http://localhost:80/", "https://localhost:443/"));
});
var tokenOptions = Configuration.GetSection("TokenOptions").Get<TokenOptions>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login/";
});
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}
)
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.IncludeErrorDetails = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidIssuer = tokenOptions.Issuer,
ValidAudience = tokenOptions.Audience,
ValidateIssuerSigningKey = true,
IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey),
ClockSkew = TimeSpan.Zero
};
});
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder(CookieAuthenticationDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build();
});
services.AddMvc().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddMvc(option => option.EnableEndpointRouting = false)
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
}
// 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");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors(builder => builder.WithOrigins("http://localhost:80", "https://localhost:443").AllowAnyHeader());
app.UseAuthentication();
app.UseAuthorization();
app.UseNToastNotify();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
}
}
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class AccountController : Controller
{
private readonly IKisiService _kisiService;
private readonly IAuthService _authService;
private readonly ITokenHelper _tokenHelper;
public AccountController(IKisiService kisiService, IAuthService authService, ITokenHelper tokenHelper)
{
_kisiService = kisiService;
_authService = authService;
_tokenHelper = tokenHelper;
}
[HttpGet]
[AllowAnonymous]
public IActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public IActionResult Login(UserForLoginDto userForLoginDto)
{
Task<OrgPerson> userToLogin = _authService.CheckUserToLogin(userForLoginDto);
if (userToLogin.Result == null)
return BadRequest(userToLogin.Result);
kisi user = _kisiService.KisiGetirEPostaIle(userToLogin.Result.KullaniciAdi);
if(user==null)
return RedirectToAction("Login", "Account");
var result = _authService.CreateAccessToken(user);
ClaimsPrincipal principal = _tokenHelper.GetClaimsPrincipal(result.Token);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(principal));
if (result != null)
{
return RedirectToAction("Index", "Home");
}
return BadRequest(user);
}
}
//launchsettings.json { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:15798", "sslPort": 44300 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "LabStokTakip": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, } } }
//appsettings.json { "ConnectionStrings": { "DefaultConnection": "Server=******;Database=******;User Id=*****;Password=******;" }, "TokenOptions": { "Audience": "https://localhost:5001/", "Issuer": "https://localhost:5001/", "AccessTokenExpiration": 10, "SecurityKey": "************************************************************" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" }
我解決了這個問題。 在我的“login.cshtml”中,我使用了
<form action="/Account/Login" method="post" class="pt-2" >
我在“/”之前添加了“~”,問題解決了。
<form action="~/Account/Login" method="post" class="pt-2" >
這適用於其他菜單項鏈接為“..”而不是“~”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.