标签: asp.net-mvc-5

如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目?

假设您在创建新的MVC(5)项目时忘记勾选Web API复选框(将其添加到项目中),您需要做什么才能添加Web API并使其正常工作?

有一堆迁移问题,但似乎都没有将Web API添加到MVC 5项目的完整和最新步骤,而且似乎已经从一些旧的答案中改变了.

将Web API添加到MVC 4

添加GlobalConfiguration.Configure(WebApiConfig.Register)MVC 4

c# asp.net asp.net-mvc asp.net-web-api asp.net-mvc-5

141
推荐指数
1
解决办法
9万
查看次数

Visual Studio 2013 IntelliSense停止为ASP.NET MVC5控制器工作

我在Visual Studio 2013 ASP.NET MVC 5项目中遇到了一个奇怪的问题.突然之间,MVC 5项目的Controller类中的IntelliSense根本不起作用.

我的解决方案中有其他类库项目,IntelliSense正在这些类库项目文件中工作,但问题只出在MVC项目中.通常通过重新启动Visual Studio来解决这些问题:我不仅重新启动Visual Studio,而且还重新启动了计算机,并且IntelliSense问题根本没有得到修复.

还有其他步骤可以让我恢复智能感知吗?

intellisense visual-studio-2013 asp.net-mvc-5

132
推荐指数
7
解决办法
10万
查看次数

如何扩展User.Identity的可用属性

我正在使用MVC5 Identity 2.0登录我的网站,其中身份验证详细信息存储在SQL数据库中.Asp.net Identity已经以标准方式实现,可以在许多在线教程中找到.

IdentityModels中的ApplicationUser类已扩展为包含一些自定义属性,例如整数OrganizationId.我们的想法是,可以创建许多用户并将其分配给公共组织以实现数据库关系.

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        //Extended Properties
        public DateTime? BirthDate { get; set; }
        public long? OrganizationId { get; set; }

        //Key Mappings
        [ForeignKey("OrganizationId")]
        public virtual Organization Organization { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

如何从控制器中检索当前登录用户的OrganizationId属性?一旦用户登录,这是否可通过方法获得,或者每次执行控制器方法时,我是否始终根据UserId从数据库中检索OrganizationId?

在网上阅读我看到我需要使用以下内容来登录UserId等.

using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)

但是,OrganizationId不是User.Identity中可用的属性.我是否需要扩展User.Identity以包含OrganizationId属性?如果是这样,我该怎么做呢.

我经常需要OrganizationId的原因是许多表查询依赖于OrganizationId来检索与登录用户相关联的与组织相关的数据.

asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2

125
推荐指数
4
解决办法
10万
查看次数

如何在ASP.NET MVC 5和WEB API 2中实现oauth2服务器

首先,我将描绘我的项目:

对于我的实习船,我需要为现有系统添加功能.一旦用户通过oauth2授权,第三方客户端必须能够访问AX Web服务中的数据.我知道我需要建立一个"代理Web服务",客户端可以在其中进行调用并调用AX服务,但我对oauth2部分有点不确定.大多数教程和指南都是关于在Facebook或谷歌登录中使用ASP.NET的标识.我不需要,我需要使用现有的凭据,所以我需要自己创建oauth2服务.

我发现很难找到关于此的教程,指南或解释.我理解oauth2以及需要做什么,但我以前从未做过这样的事情并且发现它很难开始.我发现的最接近我需要的是这个github repo链接,但解决方案没有构建.

我想到的是建立一个ASP.NET MVC网站,客户(第三方)可以自己注册并获取他们的客户ID.使用ASP.NET API,我想创建获取所需令牌和参数的API,然后访问Dyn AX服务.

这是正确的还是我完全错了?有关构建您自己的oauth2服务器/服务的任何帮助或链接都会很好.

asp.net oauth-2.0 asp.net-mvc-5 asp.net-web-api2

124
推荐指数
3
解决办法
15万
查看次数

使用与MVC5异步的优势是什么?

有什么区别:

public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        IdentityResult result = IdentityManager.Authentication.CheckPasswordAndSignIn(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
        if (result.Success)
        {
            return Redirect("~/home");
        }
        else
        {
            AddErrors(result);
        }
    }
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

和:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        IdentityResult result = await IdentityManager.Authentication.CheckPasswordAndSignInAsync(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
        if (result.Success)
        {
            return Redirect("~/home");
        }
        else
        {
            AddErrors(result);
        }
    }
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

我看到MVC代码现在有异步,但有什么区别.一个人的表现比另一个人好得多吗?调试一个问题比另一个问题更容易吗?我是否应该为我的应用程序更改其他控制器以添加异步?

asp.net-mvc task-parallel-library async-await asp.net-mvc-5 asp.net-identity

116
推荐指数
1
解决办法
5万
查看次数

如何在Visual Studio 2012中安装ASP.NET MVC 5?

有没有办法在Visual Studio 2012中安装ASP.NET MVC 5?

我尝试过使用NuGet(http://www.nuget.org/packages/Microsoft.AspNet.Mvc).但我认为它应该作为Visual Studio扩展,但我无法在任何地方找到它.

或者它仅在Visual Studio 2013中可用?

visual-studio-2012 asp.net-mvc-5

114
推荐指数
6
解决办法
16万
查看次数

MVC 5访问声明身份用户数据

我正在使用Entity Framework 5 Database First方法开发MVC 5 Web应用程序.我正在使用OWIN进行用户身份验证.下面显示我的帐户控制器中的登录方法.

public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
        if (user != null)
        {
            var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);

            identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));
            identity.AddClaim(new Claim(ClaimTypes.GivenName, "A Person"));
            identity.AddClaim(new Claim(ClaimTypes.Sid, user.userID)); //OK to store userID here?

            AuthenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = model.RememberMe
            }, identity);

            return RedirectToAction("Index", "MyDashboard");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password."); …
Run Code Online (Sandbox Code Playgroud)

asp.net authentication asp.net-mvc claims-based-identity asp.net-mvc-5

109
推荐指数
9
解决办法
18万
查看次数

如何在asp.net MVC 4和MVC 5中设置默认控制器

如何在不使用HomeController的情况下为我的ASP.NET MVC 4项目设置默认控制器?

应用程序启动时如何设置默认区域

c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

106
推荐指数
3
解决办法
16万
查看次数

EntityType'IdentityUserLogin'没有定义键.定义此EntityType的键

我正在使用Entity Framework Code First和MVC 5.当我使用个人用户帐户身份验证创建我的应用程序时,我获得了一个帐户控制器以及所有必需的类和代码,以使Indiv用户帐户身份验证工作.

已经存在的代码包括:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DXContext", throwIfV1Schema: false)
    {

    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
Run Code Online (Sandbox Code Playgroud)

但后来我继续使用代码创建了我自己的上下文,所以我现在也有以下内容:

public class DXContext : DbContext
{
    public DXContext() : base("DXContext")
    {

    }

    public DbSet<ApplicationUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<Artist> Artists { get; set; }
    public DbSet<Paintings> Paintings { get; set; }        
}
Run Code Online (Sandbox Code Playgroud)

最后,我有以下种子方法为我添加一些数据,同时开发:

protected override void Seed(DXContext context) …
Run Code Online (Sandbox Code Playgroud)

seeding ef-code-first asp.net-mvc-5

98
推荐指数
4
解决办法
10万
查看次数

MVC 5种子用户和角色

我一直在玩新的MVC 5,我有一些模型,控制器和视图设置使用代码优先迁移.

我的问题是如何为用户和角色提供种子?我目前在Configuration.cs中的Seed方法中播放一些参考数据.但是在我看来,在第一次点击AccountController之前,不会创建用户和角色表.

我目前有两个连接字符串,所以我可以将我的数据从我的身份验证分成不同的数据库.

如何让用户,角色等表格与我的其他人一起填充?而不是当帐户控制器被击中?

asp.net-mvc seeding ef-migrations asp.net-mvc-5

91
推荐指数
4
解决办法
6万
查看次数