标签: asp.net-identity-2

UserManager.CreateAsync不生成Id

我刚刚将我的MVC 5应用程序(之前是MVC 3 SimpleMembership)升级到ASP.NET Identity 2.0,我可以与现有用户一起工作,但是当我执行以下操作来创建新用户时:

var user = new ApplicationUser();
//user.Id = db.Users.Max(u => u.Id) + 1;    //does not help
user.UserName = model.UserName;
user.Email = model.EMailAddress;
var result = await UserManager.CreateAsync(user, model.Password);
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

System.Data.Entity.Core.UpdateException:更新条目时发生错误.有关详细信息,请参阅内部异常 ---> System.Data.SqlClient.SqlException:无法将值NULL插入列'Id',表'MyDB.dbo.AspNetUsers'; 列不允许空值.INSERT失败.该语句已终止.

有谁知道为什么会这样?

这是我的UserManager:

var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<MyDbContext>()));

manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
{
    AllowOnlyAlphanumericUserNames = false,
    RequireUniqueEmail = true
};

manager.PasswordValidator = new PasswordValidator
{
    RequiredLength = 6,
    RequireNonLetterOrDigit = false,
    RequireDigit = false,
    RequireLowercase = false,
    RequireUppercase …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework asp.net-identity-2

3
推荐指数
1
解决办法
6133
查看次数

从ASP.NET Identity 2.x中的角色中删除用户

如何从ASP.NET Identity 2.x中的角色中删除用户?关于向用户添加角色没有问题,但是当我想从用户中删除角色时我无法解决.应该提到的是没有异常或错误!

//POST: Admin/User/Edit/5
    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit([Bind(Prefix = "")]UserViewModel userViewModel, List<int> availableRoles)
    {
        if (ModelState.IsValid)
        {
            List<int> newListOfRolesIDs = availableRoles;
            List<int> oldListOfRolesIDs = UserBLL.Instance.GetRolesIDs(userViewModel.Id);
            List<int> deletedList;
            List<int> addedList;
            var haschanged = oldListOfRolesIDs.ChangeTracking(newListOfRolesIDs, out deletedList, out addedList);
            using (new EFUnitOfWorkFactory().Create())
            {
                if (haschanged)
                {
                    UserBLL.Instance.InsertRoles(addedList, userViewModel.Id);
                    UserBLL.Instance.DeleteRoles(deletedList, userViewModel.Id);
                }
                await UserBLL.Instance.UpdateAsync(userViewModel);
            }
            //ArticleBLL.Instance.UpdatePartial(articleViewModel,  m => m.Title);
            return RedirectToAction("Edit");
        }
        return View(userViewModel);
    }
Run Code Online (Sandbox Code Playgroud)

删除角色方法:

public void DeleteRoles(List<int> deleteList, int? userId)
    {
        if (userId != null)
        {
            User user …
Run Code Online (Sandbox Code Playgroud)

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

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

暂时禁用密码要求

我正在将用户帐户迁移到最少需要6个字符的应用程序.

如果我运行以下功能

var result = await UserManager.CreateAsync(newUser, "");
Run Code Online (Sandbox Code Playgroud)

它会当然抱怨密码无效.有没有办法我可以暂时忽略密码要求,这样我就可以创建我的用户?

笔记:

private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            if (_userManager == null)
            {
                this._userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            return _userManager;
        }
        private set
        {
            _userManager = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

遗憾的是,这不起作用:

this._userManager.PasswordValidator = new PasswordValidator
{
    RequiredLength = -1, // I've also tried 0
    RequireNonLetterOrDigit = false,
    RequireDigit = false,
    RequireLowercase = false,
    RequireUppercase = false,
};
Run Code Online (Sandbox Code Playgroud)

因为它声明"您的密码长度必须至少为-1个字符"

c# owin asp.net-identity-2

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

ASP.Net MVC身份无限重定向循环

我有一个ASP.Net MVC5应用程序,使用身份"开箱即用"模板,根据ASP.Net Identity 2.0.0.我需要升级它以使用最新的ASP.Net MVC模板中的新代码,即使用SignInManager类.

我在原始应用程序中的代码与最新生成的模板之间进行了一些A | B比较,并移植了所有我可以看到的不同之处.

但是,我得到一个奇怪的错误,我怀疑OWIN有关.当我尝试登录或注册时,它会触发一个Redirect循环,最终会在安全警告的情况下崩溃应用程序,因为URL查询字符串已连接到死亡状态.


该URL为: https:// localhost:44302 /帐户/登录?ReturnUrl =%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D %252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FAccount %252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%25252525252525 25252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252FAccount%25252525252525252525252525252525252FLogin%25252525252525252525252525252525253FReturnUrl%25252525252525252525252525252525253D%2525252525252525252525252525252525252FAccount% 2525252525252525252525252525252525252FLogin%252525252525252525252525252525 2525253FReturnUrl%2525252525252525252525252525252525253D%252525252525252525252525252525252525252FAccount%252525252525252525252525252525252525252FLogin

详细错误信息:

模块    RequestFilteringModule

通知    BeginRequest

Handler    ExtensionlessUrlHandler-Integrated-4.0

错误代码    0x00000000


在所有方法中引用ReturnUrl时我都使用了完全相同的设置.

在大多数情况下,我的应用程序的原始AccountController和相关的安全代码不受原始模板的影响.我的新示例应用程序在我的本地计算机上运行正常,所以我不确定它们之间的区别.

我看到过帖子暗示IIS Express配置应该受到指责,但我已经遵循了清理建议,并且也发布到Azure网站,结果相同.

我花了很多时间试图解决这个问题,并且没有取得任何成功,所以我想我会把它放在那里寻求一些建议......提前感谢任何和所有的帮助.如果您需要查看更多代码,请与我们联系.

asp.net asp.net-mvc owin asp.net-identity-2

3
推荐指数
1
解决办法
6419
查看次数

用户登录后更新声明

用户登录后是否可以更新索赔?我在我的网站上有前端和管理面板,基本上我用它claims来实现这个目的.如果用户像Susan在前端登录一样,我在我的代码中做了类似的事情:

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

userIdentity.AddClaim(new Claim("Id", this.UserName));
... //other claims here
Run Code Online (Sandbox Code Playgroud)

因此,当用户最终登录管理面板(当仍然在前端登录时)时,我只想添加更多声明,例如:

userIdentity.AddClaim(new Claim("BackEndId", this.UserName));
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

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

3
推荐指数
1
解决办法
1913
查看次数

如何在aspnetusers表中获取自定义字段?

使用CodeFirst迁移,我在VS 2013中的asp mvc模板中的标准数据库中的表aspnetusers中添加了自定义字段"Surname"(对不起"in").如果我登录,如何获得"姓氏"字段的价值?

asp.net-mvc visual-studio asp.net-identity-2

3
推荐指数
1
解决办法
4010
查看次数

User.Identity.Name为空?

我使用visual studio 2015在我的办公室PC上创建了一个简单的web api项目,Windows登录公司Domain(AD).我更改了web.config文件以进行集成身份验证 - <system.web><authentication mode="Windows" />.url http:// localhost:23151/api/Model1从数据库返回有效的xml数据.

我在IIS Express(开发服务器)设置的项目属性中将匿名身份验证更改为已禁用,并将Windows身份验证更改为已启用.

我设置了一个断点public IQuerybal<Model1> GetModel1()并检查其值User.Identity.Name并返回null

更新: 奇怪的是我在另一台PC上创建了一个新项目(仍在Visual Studio 2015中),我可以从属性中获取Windows登录信息User.Identity.Name.

更新2: 我试图将项目文件复制到新PC,它仍然遇到同样的问题.所以它应该是项目中的一些设置.很奇怪.

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

3
推荐指数
1
解决办法
6154
查看次数

防止ASP.NET Identity的UserManager自动保存

我正在尝试与ASP.NET Identity及其UserManager集成.我有自己的自定义用户对象,我已经添加到开箱即用的ApplicationUser中.UserManager有一个Create()方法(实际上它在UserManagerExtensions类中),用于创建新用户.问题是它会自动将更改保存到此处确认的数据库中.我想在保存新用户之前在同一个"事务"中执行其他操作,因此我无法让UserManager自动保存更改.

有趣的是,我正在重写DbContext中的SaveChange()方法,我正在关联到UserManager,但它永远不会在Create()中自动保存的方法中断.

有没有办法将UserManager配置为不自动保存更改.

c# asp.net asp.net-identity asp.net-identity-2

3
推荐指数
1
解决办法
1089
查看次数

尝试加载应用程序时发生以下错误. - OwinStartupAttribute.FriendlyName值

我想创建下拉列表AspNetRoles.我用这个代码:

Idnetity Conf:

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
}
Run Code Online (Sandbox Code Playgroud)

.

启动:

using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using Identity_Work.Models;

namespace Identity_Work
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc owin asp.net-identity-2

3
推荐指数
1
解决办法
8491
查看次数

将关系添加到ASP.NET Identity(数据库优先)中的ApplicationUser类

我在ASP.NET MVC应用程序中使用ASP.NET Identity(数据库优先)。我按照此处的说明使用数据库优先方法设置ASP.NET Identity。

我的AspNetUsers表与Employee表有关系(Employee表具有UserId外键,AspNetUsers实体具有ICollection<Employee>属性)。

我想将ICollection<Employee>属性添加到ApplicationUser,如下所示:

public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ICollection<Employee> Employees { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,会收到以下错误消息:

EntityType'AspNetUserLogins'没有定义键。定义此EntityType的键。AspNetUserLogins:EntityType:EntitySet'AspNetUserLogins'基于类型'AspNetUserLogins',但未定义键。

为什么会收到此错误消息?我该如何解决?

c# asp.net-mvc ef-database-first asp.net-identity-2

3
推荐指数
1
解决办法
2969
查看次数