标签: asp.net-mvc-5

OwinStartup没有开火

我让OwinStartup配置代码完美运行,然后它停止工作.不幸的是,我不确定我做了什么让它停止工作并且很难搞清楚它.

为了确保我已经涵盖了基础知识,我加倍检查以确保我拥有

[assembly:OwinStartup(typeof(WebApplication.Startup))] 
Run Code Online (Sandbox Code Playgroud)

正确分配属性并确保我没有owin的appSetting:AutomaticAppStartup设置为false所以我将一个设置为true以保证安全,因为之前没有任何内容.

<add key="owin:AutomaticAppStartup" value="true" />
Run Code Online (Sandbox Code Playgroud)

我也尝试过专门调用appSetting:

<add key="owin:appStartup" value="WebApplication.Startup" />
Run Code Online (Sandbox Code Playgroud)

在它停止工作之前,我将Microsoft.Owin.Security NuGet包升级到2.0.2,所以我尝试将它们恢复到2.0.1(这很痛苦),但它没有改变任何东西.我在项目上安装了WebActivator并使用它来引导其他东西,但我已经在新的WebApplication模板上测试了它并且它在那里工作,所以我不认为那是罪魁祸首.

我还尝试删除我的Startup类并使用Visual Studio在Add New Item中使用OWIN Startup Class类型添加一个新类,并且也没有被调用.接下来我尝试添加第二个Startup类,因为我知道如果定义了多个OwinStartup属性它将抛出异常,但它没有抛出任何异常.

不知道还有什么可以尝试.有什么想法吗?

更新

事实证明Resharper在我用它删除未使用的引用时删除了对Microsoft.Owin.Host.SystemWeb的引用.

asp.net-mvc owin asp.net-mvc-5

328
推荐指数
8
解决办法
9万
查看次数

ASP.NET MVC 5 - 身份.如何获取当前的ApplicationUser

我的项目中有一个Article实体,其中包含ApplicationUser名为的属性Author.如何获取当前记录的完整对象ApplicationUser?在创建新文章时,我必须将Author属性设置Article为当前属性ApplicationUser.

在旧的成员机制中它很简单,但在新的身份识别方法中,我不知道如何做到这一点.

我试着这样做:

  • 为身份扩展添加using语句: using Microsoft.AspNet.Identity;
  • 然后我尝试获取当前用户: ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());

但我得到以下异常:

LINQ to Entities无法识别方法'System.String GetUserId(System.Security.Principal.IIdentity)'方法,并且此方法无法转换为商店表达式.来源=的EntityFramework

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

229
推荐指数
6
解决办法
29万
查看次数

如何将MVC5添加到Visual Studio 2013?

我正在开始一个新项目,并想尝试MVC 5(我之前使用MVC 4构建了一个Web应用程序).

在Visual Studio 2013中,我单击New Project并导航到Visual C#> Web> Visual Studio 2012(即使我已经安装了VS 2013,它在菜单中显示2012),在该列表中我只有MVC 4应用程序 - 而不是5.

下拉菜单中的框架设置为.NET Framework 4.5.1 - 但仍然没有.

我检查了工具>扩展和更新,并搜索了MVC 5 - 但它没有找到任何官方的微软.

如何将MVC 5添加到Visual Studio 2013?

谢谢

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

215
推荐指数
4
解决办法
18万
查看次数

HTTP错误500.19和错误代码:0x80070021

我有一个由Visual Studio 2013构建的简单webAPI.当我从VS13运行它时效果很好但是当我在本地IIS中复制项目时它会给我以下错误.

HTTP错误500.19 - 内部服务器错误无法访问请求的页面,因为页面的相关配置数据无效.

详细错误信息:

模块 IIS Web核心

通知 BeginRequest

处理程序 尚未确定

错误代码 0x80070021

配置错误 此配置部分不能在此路径中使用.当该部分被锁定在父级别时会发生这种情况.锁定是默认情况下(overrideModeDefault ="Deny"),或由locationMode ="Deny"或遗留allowOverride ="false"的位置标记显式设置.

配置文件 \?\ C:\ inetpub\wwwroot\APITeslin\web.config

配置来源:

36:   <system.webServer>  
37:     <handlers>  
38:       <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
Run Code Online (Sandbox Code Playgroud)

iis asp.net-mvc asp.net-web-api iis-8 asp.net-mvc-5

199
推荐指数
9
解决办法
31万
查看次数

如果我正在使用OWIN Startup.cs类并在那里移动所有配置,我是否需要一个Global.asax.cs文件?

例如,在一个全新的ASP.NET MVC 5应用程序中,使用MVC w /个人帐户模板,如果我删除Global.asax.cs该类并将其配置代码移动到Startup.cs Configuration()方法如下,有什么缺点?

public partial class Startup
{
     public void Configuration(IAppBuilder app)
     {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        ConfigureAuth(app);
    }
}
Run Code Online (Sandbox Code Playgroud)

对我来说,好处是,当将ASP.NET 4应用程序升级到ASP.NET 5并使用现在必须在Startup.cs类中配置的部分时,我不会在两个看起来相关的不同类中进行依赖注入和其他配置启动和配置.

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

188
推荐指数
3
解决办法
6万
查看次数

ASP.NET身份DbContext混淆

默认的MVC 5 App附带了IdentityModels.cs中的这段代码 - 这段代码用于默认模板的所有ASP.NET Identity操作:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用带有Entity Framework的视图构建一个新控制器并在对话框中创建一个"新数据上下文...",我会为我生成这个:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class AllTheOtherStuffDbContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc entity-framework asp.net-mvc-5 asp.net-identity

185
推荐指数
4
解决办法
11万
查看次数

未经授权的webapi呼叫返回登录页面而不是401

如何配置我的mvc/webapi项目,以便从剃刀视图调用的webapi方法在未经授权时不返回登录页面?

它是一个MVC5应用程序,它也有通过javascript调用的WebApi控制器.

以下两种方法

[Route("api/home/LatestProblems")]      
[HttpGet()]
public List<vmLatestProblems> LatestProblems()
{
    // Something here
}

[Route("api/home/myLatestProblems")]
[HttpGet()]
[Authorize(Roles = "Member")]
public List<vmLatestProblems> mylatestproblems()
{
   // Something there
}
Run Code Online (Sandbox Code Playgroud)

通过以下角度代码调用:

angular.module('appWorship').controller('latest', 
    ['$scope', '$http', function ($scope,$http) {         
        var urlBase = baseurl + '/api/home/LatestProblems';
        $http.get(urlBase).success(function (data) {
            $scope.data = data;
        }).error(function (data) {
            console.log(data);
        });
        $http.get(baseurl + '/api/home/mylatestproblems')
          .success(function (data) {
            $scope.data2 = data;
        }).error(function (data) {
            console.log(data);
        });  
    }]
);
Run Code Online (Sandbox Code Playgroud)

所以我没有登录,第一个方法成功返回数据.第二种方法返回(在成功函数中)包含等效登录页面的数据.也就是说,如果你请求一个标有[授权]并且你没有登录的控制器动作,你会在mvc中获得什么.

我希望它返回401未授权,以便我可以根据用户是否登录显示不同的数据.理想情况下,如果用户已登录,我希望能够访问Controller的用户属性,以便我可以返回特定于该成员的数据.

更新:由于下面的建议似乎都不再适用(对Identity或WebAPI的更改)我在github上创建了一个原始示例,它应该说明问题.

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

176
推荐指数
8
解决办法
8万
查看次数

什么是ASP.NET Identity的IUserSecurityStampStore <TUser>接口?

看看ASP.NET Identity(ASP.NET中的新成员实现),我在实现自己的时遇到了这个界面UserStore:

//Microsoft.AspNet.Identity.Core.dll

namespace Microsoft.AspNet.Identity
{ 
    public interface IUserSecurityStampStore<TUser> :
    {
        // Methods
        Task<string> GetSecurityStampAsync(TUser user);
        Task SetSecurityStampAsync(TUser user, string stamp);
    }
}
Run Code Online (Sandbox Code Playgroud)

IUserSecurityStampStore由默认实现EntityFramework.UserStore<TUser>,实质上获取和设置TUser.SecurityStamp属性.

经过一些挖掘之后,似乎a SecurityStampGuid在关键点新生成的UserManager(例如,更改密码).

因为我在Reflector中检查这段代码,所以我无法真正破译.几乎所有的符号和异步信息都已经过优化.

此外,谷歌也没有太多帮助.

问题是:

  • 什么是SecurityStampASP.NET标识,它用于什么?
  • SecurityStamp创建身份验证cookie时,是否扮演任何角色?
  • 是否有任何需要采取的安全措施或预防措施?例如,不要将此值向下游发送给客户端?

更新(2014年9月16日)

源代码在这里:

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

168
推荐指数
3
解决办法
6万
查看次数

如何获取当前用户,以及如何在MVC5中使用User类?

  • 如何在MVC 5中获取当前登录用户的ID ?我尝试了StackOverflow的建议,但它们似乎不适用于MVC 5.
  • 另外,为用户分配东西的MVC 5最佳实践是什么?(例如,User应该有Items,我应该存储在用户IdItem?我可以扩展User与类List<Item>导航属性?

我正在使用MVC模板中的"个人用户帐户".

试过这些:

'Membership.GetUser()'为空.

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

154
推荐指数
4
解决办法
20万
查看次数

在Web API 2和.NET 4.5.1迁移之后,GlobalConfiguration.Configure()不存在

我最近开始按照本指南将我的项目迁移到.NET 4.5.1和Web Api 2.

MS开发人员Rick Anderson要求你做的第一件事就是改变:

WebApiConfig.Register(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)

GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)

在global.asax文件中.然而,当我尝试构建时,这给了我一个错误:

错误1'System.Web.Http.GlobalConfiguration'不包含'Configure'的定义

我的项目目前是MVC 5和Web Api 2和.NET 4.5.1,但我认为System.Web.Http仍然认为它是.NEt 4.0版本.

我该怎么办呢?

谢谢.

编辑:

这是我的程序集绑定:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <!--
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly> -->
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

顶部评论后评论的所有内容都因为我收到了错误:

警告2发现无法解析的同一依赖程序集的不同版本之间发生冲突.当日志详细程度设置为详细时,这些引用冲突将在构建日志中列出.

并且摆脱硬绑定正在解决这个问题.

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

143
推荐指数
6
解决办法
15万
查看次数