- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 OWIN 与 OpenIdConnect 身份验证结合使用以登录到 Azure Active Directory。我的应用程序是使用 WebApi 2 的 ASP .NET。此时前端只是简单的 HTML/Javascript。在本地运行我的应用程序时,我在 Controller 上调用 WebApi( Controller 和任何 API 都没有 [Authorize] 过滤器)并且该 API 反过来使用当前的 OWIN 上下文来挑战身份验证。此挑战按预期失败,因为我未登录并继续调用“https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id= ...”,这也是预期的。请求返回 200 状态,响应包含 Microsoft 登录页面的 HTML。但是,我的 Web 应用程序不会重定向到该登录页面,并且控制台会显示 CORS 错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:20776' is therefore not allowed access.
我用浏览器扩展强制解决了 CORS 问题,但即便如此它也没有重定向到登录页面。取而代之的是,响应像以前一样包含登录页面的来源。我在下面概述了我采取的步骤和我正在使用的代码。我错过了一步吗?
我找到的大多数文档和示例都利用了 ASP.NET MVC。我的大部分实现都基于此处找到的 MVC 示例:https://github.com/Azure-Samples/active-directory-dotnet-webapp-groupclaims
该示例在本地运行良好,我能够使用我的 AAD 凭据登录并成功调用 MS Graph API。
至于 CORS 问题,我在 MVC 示例中没有看到这一点。为什么这个 CORS 问题会出现在我当前的设置中,我该如何解决?我希望 OWIN 中间件能够解决这个问题。在 Controller 上使用 EnableCors 过滤器没有帮助,在 IAppBuilder 上启用 CORS 也没有帮助。
我期望的 Web 请求是(基于 OWIN MCV 示例):
//Call to sign in which initiates the challenge
Request URL:http://localhost:55065/Account/SignIn
Request Method:GET
Status Code:302 Found
Remote Address:[::1]:55065
Referrer Policy:no-referrer-when-downgrade
//Call to perform the challenge. Should redirect to MS login after this
Request
URL:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=[truncated]
Request Method:GET
Status Code:200 OK
Remote Address:104.42.72.20:443
Referrer Policy:no-referrer-when-downgrade
我在我的 WebApi 项目中实际看到的网络请求是:
Request URL:http://localhost:13291/api/v1/tools/user/signin
Request Method:GET
Status Code:302 Found
Remote Address:[::1]:13291
Referrer Policy:no-referrer-when-downgrade
Request URL:http://localhost:13291/api/v1/tools/user/signin
Request Method:POST
Status Code:302 Found
Remote Address:[::1]:13291
Referrer Policy:no-referrer-when-downgrade
Request URL:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=[truncated]
Request Method:OPTIONS
Status Code:200 OK
Remote Address:104.210.48.14:443
Referrer Policy:no-referrer-when-downgrade
Request URL:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=[truncated]
Request Method:GET
Status Code:200 OK
Remote Address:104.210.48.14:443
Referrer Policy:no-referrer-when-downgrade
以下是我的设置摘要以及相关代码:
代码摘录为了简洁起见,我删除了 namespace 定义和一些类定义。下面的代码来 self 创建的一个新的最小示例,用于在我正在处理的大型 Web 应用程序之外重现该问题。前端只是一个简单的 JQuery 调用。我想提供对解决方案的访问权限,但它目前包含我的 AppSecret,我认为甚至需要它来尝试重现此问题。
WebApiConfig.cs
public static HttpConfiguration Register()
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
return config;
}
Startup.cs
[assembly: OwinStartup(typeof(Startup))]
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
Startup.Auth.cs
public partial class Startup
{
//...
//Obtain various application settings
//...
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = appId,
Authority = "https://login.microsoftonline.com/common/v2.0",
PostLogoutRedirectUri = redirectUri,
RedirectUri = redirectUri, //http://localhost:13291
TokenValidationParameters = ...,
Notifications = ...,
AuthenticationFailed = ...
}
app.UseWebApi(WebApiConfig.Register());
}
}
FooController.cs
public class FooController : ApiController
{
[Route("api/v1/tools/user/signin")]
[HttpPost]
public HttpResponseMessage SignIn()
{
if (HttpContext.Current.User == null || HttpContext.Current.User.Identity.IsAuthenticated == false)
{
var properties = new AuthenticationProperties() {RedirectUri = "/"};
Request.GetOwinContext().Authentication
.Challenge(properties, OpenIdConnectAuthenticationDefaults.AuthenticationType);
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function signIn() {
$.post("/api/v1/tools/user/signin", (r) => {
}).fail((e) => {
console.log(e);
});
}
</script>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input id="signIn" type="button" value="SignIn" onclick="signIn();" />
</body>
</html>
最佳答案
https://weblog.west-wind.com/posts/2016/Sep/26/ASPNET-Core-and-CORS-Gotchas看看这个,它帮助我在我的 web api 上处理来自 angular 的跨域请求
关于c# - 带有 OWIN 的 WebApi 2 - 授权质询不会重定向到登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237133/
我想为我的 ABP 项目中的特定应用程序服务关闭自动 WebAPI 生成服务。 最佳答案 RemoteService 属性可用于将类标记为远程服务或禁用固有地实现 IRemoteService 接口(
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我有以下 Controller ,它应该接受用户名和密码作为 POST 中的有效负载。如果我将其更改为 HttpGet 就可以了。 [RoutePrefix("api")] public class
使用以下路线: routes.MapHttpRoute( name: "Set", routeTemplate: "api/set/{id}",
我正在使用 AngularJS,我正在尝试将 json 从我的服务发送到 webAPI Controller 。当我通过发送时,我在 webApi 函数的参数中收到 null。 我的功能服务是: an
据我了解,如果我有一个 ASP.NET WebApi 方法,其签名如下所示...... public HttpResponseMessage PostCustomer(Customer custome
我遇到了一个解决方案问题,我使用 Visual Studio SPA 模板中的部分在具有 Oauth 身份验证的 WebApi 中拥有帐户 Controller 。 app.UseOAuthBea
我按照此处的说明将 webApi.HelpPage 区域和 View 添加到使用 structureMap 的现有项目中 - 但是在访问/Help url 时: StructureMap Except
我有一个 WebAPI。如何返回并打开网页。例如,我想打开 CNN.com 页面。 [HttpGet] public HttpResponseMessage Get()
我想知道是否有人可以澄清这一点。我发现用法令人困惑。 链接和视频都没有回答我的问题 我知道像这样的链接 asp.net core middleware vs filters 甚至还有关于它的视频 但是
运行以下最新版本(在撰写本文时): Visual Studio 2019 16.4.5 .NET 核心 SDK 3.1.102 x64 测试的浏览器: 谷歌浏览器 80.0.3987.122 火狐 7
想法是,将有一个外部实体 (SharePoint) 调用我的 WebAPI 并传入 PDF 文件以及有关该 PDF 文件的一些额外元数据信息。我被困在如何构造 Web API 方法的签名上。这是我到目
我有一个 WebApi 服务处理来自简单表单的上传,如下所示: 但是,我不知道如何使用 HttpClient API 模拟同一
嗨,我是 Angular 的新手,现在我从一个示例登录页面开始,该页面传递包含用户名和密码的 userEntity。userEntity 是一个对象/实体是 webapi。 问题:当我为登录按钮单击
我有一个 AngularJS + MVC + WebAPI,我正在尝试:- 使用标准(个人账户)进行MVC认证;- 使用相同的用户和密码进行基于 WebAPI 的身份验证。 问题,AngularJS
Web API 的版本存在一些混淆。看看这个 Web API at NuGet , Microsoft ASP.NET Web API 2.2 5.2.3 什么?这里是没有提到 2.2 的描述 我的猜
我正在开发一个 Web 应用程序,该应用程序将 Owin 托管用于 MVC 和 WebApi 2。 我最近将 Microsoft Mvc/WebApi 包从 5.2.2 版升级到 5.2.3 版,将
随着Web技术的发展,现在各种框架,前端的,后端的,数不胜数。全栈工程师的压力越来越大。 现在的前端的框架,既可以做各种Web,又可以做各种APP,前端框架更新换代越来越快,越来越多。 传统的模
1. WebAPI 背景知识 1.1 什么是 WebAPI JS 分成三个大的部分: ECMAScript: 基础语法部分 DOM API: 操作页面结构 BOM API: 操作浏览器 WebAPI
我是一名优秀的程序员,十分优秀!