asp.net MVC 和 APX 与 owin 在一个项目中

本文关键字:一个 项目 MVC net APX owin asp | 更新日期: 2023-09-27 18:32:16

我需要在我的 mvc 项目中添加一个 aspx 页面,但我总是得到 404。我发现在 web.config 中有 owin http 处理程序注册

 <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" />

如果我对此发表评论,我的 aspx 页面工作正常。那么哪里会出问题呢?

这是我的初创公司的代码:

public void Configuration(IAppBuilder app)
        {
            app.UseStaticFiles();
            Dictionary<string, string> mapping = new Dictionary<string, string>
                          {
                              {".apk", "application/vnd.android.package-archive"}
                          };
            StaticFileOptions options = new StaticFileOptions
                          {
                              ContentTypeProvider = new FileExtensionContentTypeProvider(mapping)
                          };
            app.UseStaticFiles(options);
LogProvider.SetCurrentLogProvider(new Log4NetLogProvider());
            HttpConfiguration config = new HttpConfiguration();
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder
            {
                Namespace = "blabla",
                ContainerName = "blabla"
            };
            builder.EntitySet<Common.DataAccess.Sharepoint.Users>("Users");
            var departmentSet = builder.EntitySet<Common.DataAccess.Sharepoint.Departments>("Department");
            builder.EntitySet<Common.DataAccess.Sharepoint.Roles>("Roles");
            var dependentFunction = departmentSet.EntityType.Collection.Function("GetChilds");
            dependentFunction.Parameter<int>("parent");
            dependentFunction.ReturnsCollectionFromEntitySet<Common.DataAccess.Sharepoint.Departments>("Department");
            var allUsers = departmentSet.EntityType.Collection.Function("GetAllDependenUsers");
            allUsers.Parameter<int>("department");
            allUsers.Parameter<string>("query");
            allUsers.ReturnsCollectionFromEntitySet<Common.DataAccess.Sharepoint.Users>("Users");
            config.MapODataServiceRoute("orgstructure", "orgstructure", builder.GetEdmModel());
            app.UseWebApi(config); 
            AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
            JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
            SecureString password = new SecureString();
            foreach (char c in "idsrv3test")
                password.AppendChar(c);
            IdentityServerOptions identityServerOptions = new IdentityServerOptions
            {
                SiteName = "123123",
                Factory = new IdentityServerServiceFactory
                {
                    UserService = new Registration<IUserService>(typeof (IdentityUserService)),
                    ScopeStore = new Registration<IScopeStore>(typeof (IdentityScopeStore)),
                    ClientStore = new Registration<IClientStore>(typeof (IdentityClientStore))
                },
                RequireSsl = false,
                CspOptions = new CspOptions
                {
                    Enabled = false
                },
                SigningCertificate = new X509Certificate2(Assets.idsrv3test, password),
                CorsPolicy = CorsPolicy.AllowAll,
                AuthenticationOptions = {EnablePostSignOutAutoRedirect = true}
            };
            identityServerOptions.Factory.Register(new Registration<IUserRoleService, UserRolesService>());
            identityServerOptions.Factory.Register(new Registration<IUserDepartmentsService, UserDepartmentsService>());
            app.Map("/open_id", idsrvApp =>
            {
                identityServerOptions.Factory.ConfigureDefaultViewService(new DefaultViewServiceOptions
                {
                    CacheViews = true,
                    ViewLoader = new Registration<IViewLoader, IdentityViewLoader>()
                });
                idsrvApp.UseIdentityServer(identityServerOptions);
            });
            app.UseCookieAuthentication(new CookieAuthenticationOptions {AuthenticationType = "Cookies"});
            string baseUrl = ConfigurationManager.AppSettings["RedirectUrl"];
            string clientName = ConfigurationManager.AppSettings["ClientName"];
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(Constants.StandardScopes.OpenId);
            stringBuilder.Append(" ");
            stringBuilder.Append(Common.Constants.Scopes.Portal);
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                Authority = baseUrl + "open_id",
                ClientId = clientName,
                Scope = stringBuilder.ToString(),
                ResponseType = "id_token",
                SignInAsAuthenticationType = "Cookies",
                RedirectUri = baseUrl,
                UseTokenLifetime = false,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        var id = n.AuthenticationTicket.Identity;
                        // create new identity and set name and role claim type
                        var nid = new ClaimsIdentity(id.AuthenticationType, ClaimTypes.GivenName, ClaimTypes.Role);
                        IEnumerable<Claim> claims = id.Claims
                            .Where(x => x.Type != Common.Constants.Claims.Role && x.Type != Common.Constants.Claims.PortalRole);
                        nid.AddClaims(claims);
                        nid.AddClaims(id.Claims.Where(x => x.Type == Common.Constants.Claims.Role)
                            .Select(x => new Claim(ClaimTypes.Role, x.Value, x.ValueType, x.Issuer, x.OriginalIssuer))
                            );
                        nid.AddClaims(id.Claims.Where(x => x.Type == Common.Constants.Claims.PortalRole)
                            .Select(x => new Claim(ClaimTypes.Role, x.Value, x.ValueType, x.Issuer, x.OriginalIssuer))
                            );
                        nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
                        n.AuthenticationTicket = new AuthenticationTicket(nid, n.AuthenticationTicket.Properties);
                        return Task.FromResult(new object());
                    },
                    RedirectToIdentityProvider = n =>
                    {
                        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
                        {
                            var baseAddress = new Uri(n.ProtocolMessage.IssuerAddress);
                            if (baseAddress.Host != n.Request.Uri.Host)
                            {
                                var uriBuilder = new UriBuilder(baseAddress) {Host = n.Request.Uri.Host};
                                n.ProtocolMessage.IssuerAddress = uriBuilder.Uri.ToString();
                            }
                            var redirectBaseAddress = new Uri(n.ProtocolMessage.RedirectUri);
                            if (redirectBaseAddress.Host != n.Request.Uri.Host)
                            {
                                var uriBuilder = new UriBuilder(redirectBaseAddress) { Host = n.Request.Uri.Host };
                                n.ProtocolMessage.RedirectUri = uriBuilder.Uri.ToString();
                            }
                        }
                        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                        {
                            var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
                            n.ProtocolMessage.IdTokenHint = idTokenHint;
                        }
                        return Task.FromResult(new object());
                    }
                }
            });
}

asp.net MVC 和 APX 与 owin 在一个项目中

只要您使用 OwinHttpHandler,就无法提供 aspx 页面。发生这种情况是因为不是让 aspx 请求到达 Web 表单处理程序,而是将其强加给不知道如何处理 aspx 的 OwinHttpHandler 处理。因此,正如您根据经验看到的那样,解决方案是消除从 web.config 添加处理程序。