Tag: owin

C#:修改Owin响应流会导致AccessViolationException

我试图在特定情况下使用一些自定义Owin中间件来修改(在这种情况下,完全替换)响应流。 每当我打电话确实触发我的中间件来替换响应时,一切正常。 只有当我打电话给我的中间件没有进行更改时,才会出现此问题。 此外,我只能在未被替换的API调用返回手动创建的HttpResponseMessage对象时发生错误。 例如,调用此API: public class testController : ApiController { public HttpResponseMessage Get() { return Request.CreateResponse(HttpStatusCode.OK,new { message = “It worked.” }); } } 工作正常,但这个类: public class testController : ApiController { public HttpResponseMessage Get() { HttpResponseMessage m = Request.CreateResponse(); m.StatusCode = HttpStatusCode.OK; m.Content = new StringContent(“It worked.”, System.Text.Encoding.UTF8, “text/plain”); return m; } } 导致错误发生。 […]

我如何要求Owin / Katana将标头写入输出流?

当我写回复时,Katana会跳过发送Elapsed-Time响应头。 在我第一次写入流之前,如何让它为我设置标题? 中间件#1 public override async Task Invoke(IOwinContext context) { var stopwatch = new Stopwatch(); stopwatch.Start(); await Next.Invoke(context); stopwatch.Stop(); context.Response.Headers.Add(“Elapsed-Time”, new[] {stopwatch.ElapsedMilliseconds.ToString()}); } 中间件#2 public override async Task Invoke(IOwinContext context) { await context.Response.WriteAsync(“test”); }

Mvc,授权反弹授权用户

我试图将MVC 5网页的一部分限制为某个Active目录组的用户,但[Authorize]属性(在控制器上)阻止登录用户。 我的登录页面代码如下所示: public class AccountController: Controller { [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { ActiveDirectoryHelper ad = new ActiveDirectoryHelper(); if (Membership.ValidateUser(model.UserName, model.Password)) { if (ad.CheckGroupMembership(model.UserName)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > […]

Owin / Katana应该取代Web API吗?

当ASP.NET MVC出现时,微软在许多地方多次宣布它不应该取代ASP.NET Web Forms。 换句话说,它只是您可能觉得有用的另一种技术,或者您可能在其他场景中使用Web窗体。 然而,随着公司进入市场,他们无法拥有技术丛林,因为这太昂贵了。 他们通常选择一种成熟的技术,坚持使用它,在它上面构建并扩展它并在其中重复使用元素以降低成本。 现在我们正试图决定从Web API迁移到Owin / Katana。 我们只是想知道我们100%搬到Owin是否可以? 我问这个问题的原因是因为我们为Web API创建了一个非常丰富的代码库,包括流式传输,压缩,身份validation,UGC规范化,I18N和L10N支持等等。 如果我们想要迁移到Owin,我们需要再次为Owin重新创建这些工具/实用程序,因为它的体系结构与Web API不同。 我们想转移到Owin,因为它是更快,更轻,自托管的服务器,似乎是微软服务技术的未来。 我们完全转移到Owin并想象通过Owin提供所有服务的未来是否安全,我们是否停止使用Web API?

为什么所需的Startup类不需要实现适当的接口,比如IStartup?

使用katana,为什么Startup类不应该实现相应的接口,例如: interface IStartup { void Configuration(IAppBuilder app); } public class MyStartup : IStartup { public void Configuration(IAppBuilder app) { … } } 我认为开发人员可以更直观地理解他们应该将WebApp.Start方法作为T参数提供什么而不是猜测和查找示例,它应该更明确: public void Start() where T : IStartup

将HttpRequestMessage转换为OwinRequest和OwinResponse转换为HttpResponseMessage

我有一个Web API消息处理程序MyHandler ,我想在OWIN管道中作为中间件运行。 所以像这样配置处理程序。 public class Startup { public void Configuration(IAppBuilder app) { app.UseHttpMessageHandler(new MyHandler()); HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( “DefaultWebApi”, “{controller}/{id}”, new { id = RouteParameter.Optional }); app.UseWebApi(config); } } 处理程序非常简单,什么都不做。 public class MyHandler : DelegatingHandler { protected override async Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // <— breakpoint here var response […]

捕获登录事件,以便我可以缓存其他用户信息

我已经构建了一个Web应用程序。 当我建立它时,我勾选了“组织账户” 它运行良好 – 我使用我的Office 365帐户登录, User.Identity.Name包含电子邮件地址 此应用程序是旧的ASP Classic应用程序的替代前端。 该应用程序有一个我需要使用的现有安全表。 我想使用电子邮件地址查找此表中的记录以获取 用户的内部数据库密钥(因此我可以在数据库调用中使用它) 用户的安全级别(授权) 我想在validation后立即查看,并将这两个值保存到Session以供稍后参考 我有一个现有的方法来执行所有这些查找和缓存。 我实际上通过从_LoginPartial.cshtml视图调用它来实现它,但显然从视图中触发这种事情是不正确的 这是查找和缓存用户信息的代码。 现在这是在AccountController.cs但它不一定是 private Boolean GetAdditionalUserInfo() { // if authentication info is saved, don’t go find it if (Session[“UID”] != null) return true; // get the db employee id from the database and save it to the session var r […]

OWIN OpenIdConnect中间件IDX10311随机数无法validation

我有一个使用OWIN中间件的应用程序用于OpenIdConnect。 startup.cs文件使用app.UseOpenIdConnectAuthentication的标准实现。 cookie被设置为浏览器,但它出错: IDX10311:RequireNonce为’true’(默认值),但validationContext.Nonce为null。 nonce无法validation。 如果您不需要检查现时,请将OpenIdConnectProtocolValidator.RequireNonce设置为“false”。 我发现当像大多数调试项目一样运行fiddler时,会发生这种情况。 返回错误,但如果我返回该站点,一切正常,我的用户已通过身份validation。 有没有人在运行提琴手时看到这种行为? 随着提琴手: OpenIdConnect中的SecurityTokenValidated通知执行两次。 第二次通过IDX10311后抛出错误 浏览器包含有效的cookie,返回页面我可以查看有效的User.Identity数据。 没有提琴手跑: SecurityTokenValidated在OpenIdConnect中执行一次 没有抛出错误,继续加载控制器操作以进行后validation重定向Uri Cookie也有效且User.Identity数据正确。 想法? 我可以在不运行fiddler的情况下绕过它,但是在调试时也可以运行fiddler来检查流量。

Hangfire Dashboard授权配置不起作用

我已经下载了nu-get软件包Hangfire.Dashboard.Authorization 我正在尝试按照以下文档配置基于OWIN的授权,但我得到intellisense错误DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead 我也得到intellisense错误The type or namespace AuthorizationFilter and ClaimsBasedAuthorizationFilterd not be found using Hangfire.Dashboard; using Hangfire.SqlServer; using Owin; using System; namespace MyApp { public class Hangfire { public static void ConfigureHangfire(IAppBuilder app) { GlobalConfiguration.Configuration .UseSqlServerStorage( “ApplicationDbContext”, new SqlServerStorageOptions { QueuePollInterval = TimeSpan.FromSeconds(1) }); var options = new DashboardOptions […]

UserManager继续抛出System.ArgumentNullException

我试图在AspNetUsers从nvarchar到int的标准Id 。 我已经设法让那一方工作了。 但是我的问题是当我尝试登录时,我不断从UserManager类中收到错误。 我的代码如下: public class UserManager : UserManager { public UserManager(IUserStore store) : base(store) { } 在我登录的页面上 if (IsValid) { // Validate the user password var manager = Context.GetOwinContext().GetUserManager(); var user = manager.Find(UserName.Text, Password.Text); //This line throws the error if (user != null) { IdentityHelper.SignIn(manager, user, isPersistent: false); Response.Redirect(“~/Home.aspx”); } else { FailureText.Text […]