C# MVC应用程序在登录后重定向到错误页面,我不知道为什么
我有一个MVC应用程序,要求用户使用其microsoft帐户登录,在本地主机上测试时效果良好,但是在将其发布到Azure时,用户登录后会被重定向到C# MVC应用程序在登录后重定向到错误页面,我不知道为什么,c#,asp.net,asp.net-mvc,azure,azure-web-app-service,C#,Asp.net,Asp.net Mvc,Azure,Azure Web App Service,我有一个MVC应用程序,要求用户使用其microsoft帐户登录,在本地主机上测试时效果良好,但是在将其发布到Azure时,用户登录后会被重定向到http://{appname}.azurewebsites.net/home/error,这导致找不到404,因为我没有在我的家庭控制器上实现错误方法。但我的问题是,我不知道是什么原因导致应用程序重定向到此错误页面。试图在浏览器上手动输入http://{appname}.azurewebsites.net/home,只会再次重定向到错误页面 我试着在
http://{appname}.azurewebsites.net/home/error
,这导致找不到404,因为我没有在我的家庭控制器上实现错误方法。但我的问题是,我不知道是什么原因导致应用程序重定向到此错误页面。试图在浏览器上手动输入http://{appname}.azurewebsites.net/home
,只会再次重定向到错误页面
我试着在home controller上创建一个错误方法,在屏幕上简单地显示一个“hello”,然后发生了一些非常奇怪的事情,应用程序在错误页面url和windows登录页面之间来回重定向,而实际上也没有打开(我可以看到URL在浏览器上来回切换,但这两个页面都没有打开,因为它在加载完成之前重定向了)
然后,我将主控制器上的方法基本上保留为空,正如您在下面所看到的,只剩下VisualStudio添加的默认内容,但同样的问题仍在发生
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
在web.config上,我有
,所以我右键单击了错误页面并单击了ShowSource,在html之后,这里列出了这个异常堆栈跟踪:
[HttpException]: A public action method 'Error' was not found on controller 'LogDatabaseApi.Controllers.HomeController'.
at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
显然,OWIN没有正确检测到startup类,并引发了一个导致整个问题的异常。将
添加到web.config文件中的appsettings解决了这个问题您的路由是什么样子的?如果您导航到/home/index
?@DavidG,就像我在帖子中所说的那样,手动输入/h浏览器上的ome地址重定向到/home/error。在我的RouteConfig.cs
中,我有routes.MapRoute(名称:“Default”,url:{controller}/{action}/{id},默认值:new{controller=“home”,action=“Index”,id=UrlParameter.Optional});
您的授权设置如何?您是否也连接了Azure数据库?@DavidG是的,我有一个Azure数据库连接。我的授权设置基本上是VS在使用“读取目录”创建多租户项目时创建的默认设置选项已启用,可为graph API生成令牌。我已将startup.auth.cs类文件添加到帖子中。因此,您的身份验证在某个地方失败,您需要对此进行调试。您可以在AuthenticationFailed
委托中看到,该委托重定向到/home/error
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
private string resourceEndPoint = "https://manage.office.com";
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private string authority = aadInstance + "common";
private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions { });
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
// instead of using the default validation (validating against a single issuer value, as we do in line of business apps),
// we inject our own multitenant validation logic
ValidateIssuer = false,
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
return Task.FromResult(0);
},
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, resourceEndPoint);
AdminLogin.AdminOrganization = new Organization
{
Id = Guid.Parse(tenantID),
Name = "Unknown",
AccessToken = result.AccessToken,
TokenExpiration = result.ExpiresOn.DateTime,
RefreshToken = result.RefreshToken
};
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
context.OwinContext.Response.Redirect("/Home/Error");
context.HandleResponse(); // Suppress the exception
return Task.FromResult(0);
}
}
});
}
}