Tag: asp.net mvc 5

如何使用Entity Framework执行原始SQL查询而不必使用模型?

我正在尝试学习C#ASP.NET MVC 5.我正在尝试使用Entity Framework来完成我所做的一切。 但是,我需要运行原始SQL查询并将结果返回到数组中。 这是我到目前为止所做的。 我创建了我的上下文类,它允许我连接到服务器,它还允许我在运行时更改数据库。 这是我的上下文类 using ScripterEngine.Models; using System; using System.Collections.Generic; using System.Data.Common; using System.Data.Entity; using System.Data.Entity.Core.EntityClient; using System.Data.SqlClient; using System.Linq; using System.Web; namespace ScripterEngine.DataAccessLayer { public class BaseContext : DbContext { protected string connectionName; public DbSet Campaign { get; set; } /** * Created the connection to the server using the […]

在使用facebook或twitter授权后,存储并检索为用户收到的令牌

嗨我正在尝试编写一个MVC5应用程序,该应用程序能够存储在使用facebook或twitter授权后为用户收到的令牌。 我想将它存储在数据库而不是cookie中,并将其用于用户未来的任何API请求和登录尝试,以便每当他们尝试使用Facebook登录时,都不会提示他们使用Facebook或Twitter进行授权。方法或尝试访问个人资料信息。 查看成员资格数据库,我可以看到有一个名为AspNetTokens的表没有被使用 – 所以有什么我错过了我可以使用它的地方吗?

Google的间歇性ASP.NET oAuth问题,AuthenticationManager.GetExternalIdentityAsync返回null

我正在尝试使用Google作为外部登录提供程序来解决间歇性问题。 尝试登录时,会将用户重定向回登录页面,而不是进行身份validation。 问题出现在这一行(下面的链接第55行),GetExternalIdentityAsync返回null。 var externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); 完整的代码是: [Authorize] public abstract class GoogleAccountController : Controller where TUser : Microsoft.AspNet.Identity.IUser { public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } public abstract UserManager UserManager { get; set; } [AllowAnonymous] [HttpGet] [Route(“login”)] public ActionResult Login(string returnUrl) { ViewData.Model = new LoginModel() { Message = TempData[“message”] […]

使用Windows身份validation时,MVC5重定向到Login.aspx

从MVC 4升级到MVC 5后,我的应用程序(从Visual Studio中启动时)会出现以下错误。 值得注意的是,我在同一个项目中托管MVC5和WebAPI2项目,因为可能存在干扰。 我还安装了dotnetopenauth nuget包(我已删除): Server Error in ‘/’ Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled […]

无法为内容类型multipart配置Web API

我正在使用Web API – Web API 2.我的基本需求是创建一个API来更新用户的配置文件。 在这里,ios和android将以multipart / form-data的forms向我发送请求。 他们会向我发送一些图像参数。 但每当我尝试创建API时,我的模型每次都变为null。 我在WebApiConfig中添加了这一行: config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(“multipart/form-data”)); 这是我的class级: public class UpdateProfileModel { public HttpPostedFileBase ProfileImage { get; set; } public string Name { get; set; } } 这是我的控制器: [Route(“api/Account/UpdateProfile”)] [HttpPost] public HttpResponseMessage UpdateProfile(UpdateProfileModel model) { } 我甚至没有在我的模型中获取参数值。 难道我做错了什么 ? 没有与此相关的答案对我有帮助。 它大约第3天,我已经尝试了几乎所有的方法。 但我无法实现它。 虽然我可以使用它,但如下所示,但这似乎不是一个好方法。 所以我避免它.. var httpRequest = HttpContext.Current.Request; […]

获取ASP.NET MVC5身份系统中的所有角色名称

MVC5使用新的身份系统。 我怎样才能获得所有角色名称? 我尝试通过IdentityStore访问它但没有成功。

MVC5:UserManager.AddToRole():“将用户添加到角色时出错:找不到UserId”?

我一直在尝试使用MVC5 / EF6并尝试使用Code-First Migrations进行新的身份validation。 解决方案中的所有内容当前正在构建,我可以添加Migration ,但是当我通过VS2013中的package manager console执行update-database ,我的Configuration.cs文件无法将我的测试数据完全处理到我的表中并输出Error Adding User to Role: UserId not found 。 我已经尝试显式设置用户ID并让它由Manager生成(如某些示例所示),但每次收到相同的错误消息。 我知道我的Configuration.cs文件的#region User & User Roles中的错误是失败的,但我不确定原因: using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using PersonalPortfolio2.Helper; using PersonalPortfolio2.Models; using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Diagnostics; using System.Linq; namespace PersonalPortfolio2.Models { public sealed class Configuration : DbMigrationsConfiguration { public Configuration() […]

MVC – 混合身份validation – OWIN + Windows身份validation

我需要同时具有Windows身份validation和owin(表单)身份validation,但我无法让它工作。 可能最好的选择是让两个站点具有不同的身份validation方法。 我找到了一个能满足我需求的项目: MVC5-MixedAuth 。 但它使用IISExpress,我无法让它与本地IIS一起使用。 发生的错误是: 在Web服务器上配置请求筛选以拒绝请求,因为查询字符串太长。 如果我删除Startup.Auth.cs中的所有ConfigureAuth()方法,它不会抛出错误但我无法登录,因为它需要进行CookieAuthentication 。 Startup.Auth.cs: public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(dbEmployeePortal.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationSignInManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity ( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())) ) } }); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); […]

为Web Api 2和OWIN令牌身份validation启用CORS

我有一个ASP.NET MVC 5 webproject(localhost:81),它使用Knockoutjs从我的WebApi 2项目(localhost:82)调用函数,以便在我启用CORS的两个项目之间进行通信。 到目前为止,一切都有效,直到我尝试对WebApi实现OWIN令牌认证。 要在WebApi上使用/ token端点,我还需要在端点上启用CORS,但经过几个小时的尝试和搜索解决方案后,它仍在运行,并且api / token仍然会导致: XMLHttpRequest cannot load http://localhost:82/token. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); TokenConfig.ConfigureOAuth(app); … } TokenConfig public static void ConfigureOAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext(AppUserManager.Create); OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString(“/token”), AccessTokenExpireTimeSpan […]

如何在代码第一个ASP.NET MVC 5应用程序中正确设置一对多关系中的外键约束

所以我有一个文档表和ApplicationUser表。 并且一个用户可以使用代码第一种方法上传多个文档和外键约束。 但问题是我能够保存文档而无需在Documents表中分配UserID外键ID 。 我在这里想念的是什么 以下是我的新手尝试。 ApplicationUser模型: 它是框架提供的标准模型。 文件型号: public class Document { public int Id { get; set; } [StringLength(255)] public string FileName { get; set; } [StringLength(100)] public string ContentType { get; set; } public byte[] Content { get; set; } [StringLength(255)] public string DocumentName { get; set; } public string UserId { […]