Tag: owin

Web API 2 + MVC 5共享标识表

我有一个Web API和MVC5共享相同的身份表。 属于Web API的Context: public partial class CrestfineContext: IdentityDbContext { public CrestfineContext(): base(“WelbeckDatabase”, throwIfV1Schema: false) { } protected override void OnModelCreating(DbModelBuilder builder) { builder.Conventions.Remove(); builder.Entity().ToTable(“UserClaim”).HasKey(r => r.Id); builder.Entity().ToTable(“UserLogin”).HasKey(l => l.UserId); builder.Entity().ToTable(“Role”).HasKey(r => r.Id); builder.Entity().ToTable(“User”).HasKey(r => new{ r.IDNumber, r.UserName}); builder.Entity().ToTable(“User”).HasKey(r => r.UserName); builder.Entity().ToTable(“UserRole”).HasKey(r => new { r.RoleId, r.UserId });/**/ } 属于Web应用程序(MVC 5)的Context是: public partial class CrestfineContext […]

无法设置OWIN + OneLogin + Bearer

我正在尝试使用授权令牌来保护我的WebAPI项目。 我不想使用cookie,我只想使用这样的Authorization标头: Authorization: Bearer xxx_access_or_id_token_xxx 。 我正在使用OneLogin OIDC作为外部提供商。 这是我的Startup.cs using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.DataHandler.Encoder; using Microsoft.Owin.Security.Jwt; using Owin; using System.Web.Http; public void Configuration(IAppBuilder app) { var issuer = “https://openid-connect.onelogin.com/oidc/”; var audience = ConfigurationManager.AppSettings[“OneLoginClientId”]; var secret = TextEncodings.Base64.Encode((TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings[“OneLoginClientSecret”]))); app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions { AuthenticationMode = AuthenticationMode.Active, AllowedAudiences = new[] { audience }, IssuerSecurityTokenProviders = new[] { new […]

Web API令牌身份validation

我将用户凭据从Web应用程序发布到web api,该Web api实现了对用户进行身份validation并使用有效令牌进行响应的提供程序。 这是发布的方法: public TokenModel RequestAPIToken(string username, string password) { var postData = new Dictionary(); postData.Add(“grant_type”, “password”); postData.Add(“username “, username); postData.Add(“password “, password); HttpContent content = new FormUrlEncodedContent(postData); _response = _client.PostAsync(“token”, content).Result; var result = _response.Content.ReadAsAsync().Result; return result; } 这取自web api项目: public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add(“Access-Control-Allow-Origin”, new[] { “*” }); var […]

OWIN Cookie身份validation

我似乎无法让OWIN使用基于Cookie的身份validation。 我已在Startup中将我的OWIN令牌端点配置为: OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString(“/Token”), Provider = new ApplicationOAuthProvider(PublicClientId), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; app.UseOAuthBearerTokens(OAuthOptions); 我还配置了Cookie身份validation: app.UseCookieAuthentication(new CookieAuthenticationOptions()); 现在,当我点击/token端点时,我得到响应中的承载令牌,并且还在客户端使用令牌设置了cookie。 接下来我有一个用Authorize属性修饰的控制器。 当我尝试访问任何方法时,我得到401 Unauthorized响应,即使cookie随请求一起发送。 似乎OWIN没有尊重cookie进行身份validation。 我在这里遗漏了一些东西,可能是某种类型的配置? 如果我使用bearer token设置Authorization标头,但为什么它只能用于cookie?

关于IIS和Windows身份validation的NancyFx

我已经将MVC + AngularJS应用程序转换为使用NancyFx + AngularJS(因为我并没有真正使用任何MVC的东西)。 我正在使用VS2013,它在IIS(以及我的开发环境中的IIS Express)下运行。 我可以通过检查OwinEnvironment的server.User组件来获取当前登录的用户。 我目前正在使用Microsoft.Owin.Host.SystemWeb作为大多数演示。 当我在我的模块中向Get请求添加RequiresAuthentication时,即使我已登录,我也会在IE中弹出一个输入凭据。即使我输入凭据,我只是不断获得弹出窗口,它永远不会到达网站。 我有一些问题: 1)如果使用Windows身份validation和RequiresAuthentication,我仍然需要在web.config中使用身份validation模式=“Windows”。 2)是否可以在没有Microsoft.Owin.Host.SystemWeb的情况下使用IIS以避免ASP.NET管道? 我遇到过关于Project Helios和Microsoft.Owin.Host.IIS(Nuget)的文章,但这已经有一段时间没有用过了,只是一个Alpha – 这是怎么回事? 3)使用IIS,NancyFX和Windows身份validation以及RequiresAuthentication和Roles的事实上的方法是什么? 我看了很多文章和stackoverflow问题,但还没有找到明确的答案。

为什么在HttpRequest结束后第二次创建了owin中间件

根据为什么来自Asp.Net Identity的ApplicationDbContext被创建并按照请求处理两次的问题后,我做了一些研究,为什么会发生这种情况。 我发现每个HttpRequest创建一次ApplicationDbContext但是当使用Owin管道时,将在HttpRequest结束后第二次创建Owin Middleware。 因此,当用户单击一个链接时,确实会第二次创建ApplicationDbContext从而给出每个WebRequest创建对象两次的印象。 经过大量的研究,我决定在不使用任何身份validation的情况下启动一个简单的MVC 5项目。 从NuGet添加Owin中间件后,我创建了以下Owin Middleware组件。 这基本上检查HttpContext字典中是否存在一些假对象,如果不存在则创建一个伪对象。 输出将写入调试窗口以使事情变得简单。 [assembly: OwinStartupAttribute(typeof(MvcPlain.Startup))] namespace MvcPlain { public class Startup { public static int Counter; public static string FakeKeyName; public void Configuration(IAppBuilder app) { app.Use(async (context, next) => { Debug.WriteLine(“Owin middleware entered => begin request”); FakeKeyName = “owinKey” + Counter.ToString(); var fakeKeyPresent = HttpContext.Current.Items.Contains(FakeKeyName); Debug.WriteLine(string.Format(“{0} key […]

找不到Microsoft.Owin.Testing.TestServer 404

当我向TestServer发出请求时,我得到了404响应。 我无法理解为什么,因为我使用相同的配置为普通服务器,它的工作原理。 为了发出请求,我使用了TestServer.HttpClient。

在OwinStartup之后为新租户添加Owin管道中间件

我有一个多租户应用程序,每个租户可以为WsFed或OpenIdConnect定义自己的ClientID,Authority等。 所有租户都在OwinStartup注册如下: public void Configuration(IAppBuilder app) { List WsFedTenantOptions = BuildWsFedTenantOptionsList(); List OpenIdConnectTenantOptions = BuildOpenIdConnectTenantOptionsList(); app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions() { CookieSecure = CookieSecureOption.Never }); foreach (var WsFedTenantOption in WsFedTenantOptions) app.UseWsFederationAuthentication(WsFedTenantOption); foreach (var OpenIdConnectTenantOption in OpenIdConnectTenantOptions) app.UseOpenIdConnectAuthentication(OpenIdConnectTenantOption); … } 它通过context.Authentication.Challenge(AuthenticationType)切换要使用的STS。 这工作得很好。 问题是当新租户注册时,如何在没有应用程序池回收的情况下访问IAppBuilder并添加新的AuthenticationOptions ?

一小时后,身份从持有人令牌中消失

我正在使用Azure AD与Web应用程序和Web API建立多租户解决方案。 Web应用程序使用OpenIdConnect来检索承载令牌(在Azure Redis缓存中缓存),该令牌在Angular中用于从Web api获取JSON。 在Web应用程序和Web API(在Azure AD应用程序中设置)之间使用用户模拟。 问题: 这可以正常工作大约一个小时,然后身份突然消失在web api端。 如果我刷新Web应用程序,我会看到该页面被重定向到Microsoft登录页面,但不需要执行任何操作,因为用户只是重定向回Web应用程序并且一切都有效。 据我所知,Web应用程序在失败时使用相同的承载令牌,在再次运行时刷新(相同的过期时间)。 AuthenticationContext.AcquireTokenSilent适用于这两种情况。 我试图增加很多不同的超时,但没有任何帮助。 我还在web api上禁用了除持有者令牌身份validation之外的所有身份validation。 我不明白为什么身份会消失,为什么它有助于刷新客户。 有任何想法吗? 🙂 附加信息 这是RequestContext.Principal.Identity在登录或刷新后大约一小时查找的内容(在web api上): 这是大约一个小时后,导致身份validation失败: 我尝试过的一些代码更改: 在web api HttpConfiguration中: config.SuppressDefaultHostAuthentication(); config.Filters.Add( new HostAuthenticationFilter( new WindowsAzureActiveDirectoryBearerAuthenticationOptions().AuthenticationType)); 这将未经身份validation的主体从WindowsPrincipal更改为ClaimsPrincipal,但在一小时后仍然失败。 WindowsAzureActiveDirectoryBearerAuthenticationOptions BackChannelTimeout set to 5 days. Still fails 在Web应用程序web.config中: sessionState timeout=”525600″ for RedisSessionStateProvider. Still fails 在web应用程序owin auth进程中,增加了时间跨度并增加了滑动到期时间。 仍然失败: app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); […]

自定义Asp.net mvc 5身份validation,不使用Microsoft.AspNet.Identity.EntityFramework

如何在不使用Microsoft.AspNet.Identity.EntityFramework的UserStore的情况下创建自定义ASP.NET MVC 5 Auth?