我尝试通过只是谷歌身份验证来使用OWIN身份验证 即 - 我的应用程序的用户只有拥有谷歌帐户
才存在我已经按照以下方式配置了我的Auth配置:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
CookieName = CookieAuthenticationDefaults.CookiePrefix + "External",
ExpireTimeSpan = TimeSpan.FromMinutes(5),
LoginPath = new PathString("/authentication"),
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
{
ClientId = "xxx123",
ClientSecret = "xxx456",
});
}
}
我的AuthenticationController
有一个索引方法:
[AllowAnonymous]
public ActionResult Index()
{
Request.GetOwinContext().Authentication.Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action("ExternalLoginCallback")
});
return new HttpUnauthorizedResult();
}
当我进入限制页面时,我得到了
HTTP错误404.15 - 未找到请求过滤模块是 配置为拒绝查询字符串太长的请求。
...它多次击中我的AuthenticationControllers Index方法......
知道我没有正确配置的内容吗?
修改
我的ExternalLoginCallback
看起来像是:
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
}
注意 - 如果我在其上设置断点,则此方法永远不会被命中
答案 0 :(得分:1)
我的问题是我没有将提供程序类型传递给Challenge
方法 -
将我的索引操作方法更改为:
[AllowAnonymous]
public ActionResult Index()
{
var properties = new AuthenticationProperties
{
RedirectUri = Url.Action("ExternalLoginCallback")
};
//challenge
Request.RequestContext.HttpContext.GetOwinContext().Authentication.Challenge(properties, "Google");
//if above didn't handle it, return unauth.
return new HttpUnauthorizedResult();
}