我正在尝试将 Microsoft 帐户登录集成到我的 ASP.NET MVC 应用程序中,并且我有这个控制器方法:
public void SignIn()
{
// HACK - we will be signed into only one account if we are not signed in to MS
if (Request.GetOwinContext().Authentication.User.Identities.Count() <= 1)
{
// Signal OWIN to send an authorization request to Azure
Request.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "http://localhost:31503/MicrosoftCalendar" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
我期望发生的是系统提示我登录我的 Microsoft 帐户;相反,会发生什么是这种方法一遍又一遍地运行,什么都不做,直到我在浏览器中收到“重定向过多”错误。我怎样才能得到Challenge真正做某事的方法?
我的网络项目中有一个课程OwinStartup;我将它设置为 OWIN 启动类,如下所示:
[assembly: OwinStartup(typeof(Root.OwinStartup))]
但是由于某种原因,我在这个启动类中的断点从未被击中;OWIN 永远不会被初始化......实际上,等一下,它正在被初始化,但是事件处理程序OnAuthorizationCodeReceivedAsync永远不会被击中......
如果我单步执行代码,在方法Challenge中调用之后SignIn,我会由于某种原因被重定向到 a UserController,这反过来又将我重定向回SignIn方法。我想知道为什么我要在UserController?
编辑:我需要更多代码?好的,Global.asax.cs 中的这个方法在 OWIN 调用后立即执行:
protected void MvcApplication_BeginRequest(object sender, EventArgs e)
{
#region Set the context GUID cookie
if (null == Request.Cookies[CookieName.ContextGUID])
{
Response.SetCookie(new System.Web.HttpCookie(CookieName.ContextGUID, Guid.NewGuid().ToString()));
}
#endregion
// check to see whether SSL is required
if (System.Web.Security.FormsAuthentication.RequireSSL)
{
// check where the request is originating from
if (Request.UserHostName != "127.0.0.1" && Request.UserHostName != "localhost")
{
// check if the request is secure
if (!Request.IsSecureConnection)
{
string url = null;
// check for querystring segments
if (!String.IsNullOrEmpty(Request.ServerVariables["QUERY_STRING"]))
{
url = String.Format("https://{0}{1}?{2}",
Request.ServerVariables["SERVER_NAME"],
Request.ServerVariables["SCRIPT_NAME"],
Request.ServerVariables["QUERY_STRING"]);
}
else
{
url = String.Format("https://{0}{1}", Request.ServerVariables["SERVER_NAME"], Request.ServerVariables["SCRIPT_NAME"]);
}
// redirect to the secure url
Response.Redirect(url);
}
}
}
// verify the request
if (null != Request)
{
// NOTE: This is a workaround for the following exception thrown by the ReportViewer control when
// using a non-IE browser:
// Missing URL parameter: IterationId
// See the following reference: https://connect.microsoft.com/VisualStudio/feedback/details/556989/?wa=wsignin1.0
if (Request.Path.EndsWith("Reserved.ReportViewerWebControl.axd") &&
Request.QueryString["ResourceStreamID"] != null &&
Request.QueryString["ResourceStreamID"].ToLower().Contains("blank.gif"))
{
// intercept the request and send to actual valid image path
Response.Redirect(Constant.ImageRoot + "blank.gif");
}
}
}
不确定这是否是导致无限重定向循环的原因,但它在这里......