我试图了解[AllowAnonymous]标签的工作原理.
我有以下方法
[HttpGet]
public ActionResult Add()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
当我没有登录时,我可以打开此页面.我的印象是,只有将[AllowAnonymous]标记放在我应该能够做到的方法之上,才能做到这一点.我需要打开一个设置来使其工作吗?
我尝试了很多搜索,然后尝试了其他选项,但似乎没有任何效果。
我正在使用ASP.net Identity 2.0,并且具有UpdateProfileViewModel。更新用户信息时,我想将UpdateProfileViewModel映射到ApplicationUser(即,身份模型)。但是我想保留这些值,我是从用户数据库中获得的。即用户名和电子邮件地址,不需要更改。
我试着做:
Mapper.CreateMap<UpdateProfileViewModel, ApplicationUser>()
.ForMember(dest => dest.Email, opt => opt.Ignore());
Run Code Online (Sandbox Code Playgroud)
但是映射后我仍然将Email设置为null:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
user = Mapper.Map<UpdateProfileViewModel, ApplicationUser>(model);
Run Code Online (Sandbox Code Playgroud)
我也试过了,但是没有用:
public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
foreach (var property in existingMaps.GetUnmappedPropertyNames())
{
expression.ForMember(property, opt => opt.Ignore());
}
return expression;
}
Run Code Online (Sandbox Code Playgroud)
然后:
Mapper.CreateMap<UpdateProfileViewModel, ApplicationUser>()
.IgnoreAllNonExisting();
Run Code Online (Sandbox Code Playgroud) asp.net automapper asp.net-mvc-5 asp.net-identity asp.net-identity-2
我使用它来添加一个新创建的用户,角色:
var db = new ApplicationDbContext();
ApplicationDbContext context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, model.UserRole);
Run Code Online (Sandbox Code Playgroud)
在我的解决方案中username = email.
我发现,当用户名/电子邮件中包含一个符号(+, - 或类似的东西)时,它不会将用户添加到角色中.我得到的错误是" User name x is invalid, can only contain letters or digits.".用户成功添加,只是AddToRole,失败.
但我无法弄清楚为什么.
AllowOnlyAlphanumericUserNames 在IdentityConfig.cs中设置为false
有任何想法吗?
在我的一个应用程序中,除了在UserStore中创建新用户外,我还重写了Asp Core Identity UserManager的CreateAsync-在单独的统计信息表中(在不同的dbcontext上)创建新行。问题是,我希望将这两个操作都在事务内触发,这样,如果一个操作失败-另一个则不提交。这是代码:
public override async Task<IdentityResult> CreateAsync(TUser user, string password)
{
// We need to use transactions here.
var result = await base.CreateAsync(user, password);
if (result.Succeeded)
{
var appUser = user as IdentityUser;
if (appUser != null)
{
// create user stats.
var userStats = new UserStats()
{
ActionsCount = 0,
EventsCount = 0,
FollowersCount = 0,
ProjectsCount = 0,
UserId = appUser.Id
};
_ctx.UserStats.Add(userStats);
await _ctx.SaveChangesAsync();
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
事情是,我不知道如何设置这样的事务,因为TransactionScope似乎还不是.Net Core的一部分(?),并且当前使用HttpContext.GetOwinContext()获取base.CreateAsync()的当前作用域DbContext并不由于HttpContext似乎缺少此方法,因此无法工作(引用其他堆栈溢出答案中所暗示的Microsoft.Owin.Host.SystemWeb或Microsoft.AspNet.WebApi.Owin都行不通-两者均与.netcore不兼容)。有什么帮助吗?
我正在使用ASP.NET MVC5 Identity并尝试实现基于声明的身份验证.
我收到以下错误:
无法隐式转换类型'System.Collections.Generic.IEnumerable << anonymous type:string subject,string type,string value >>'to System.Web.Mvc.ActionResult'.存在显式转换(您是否错过了演员?)
这是一段代码:
public ActionResult GetClaims()
{
var identity = User.Identity as ClaimsIdentity;
var claims = from c in identity.Claims
select new
{
subject = c.Subject.Name,
type = c.Type,
value = c.Value
};
return claims;
}
Run Code Online (Sandbox Code Playgroud)
我正在关注http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/的一个例子
我用Identity和MySQL创建了一个MVC应用程序.我创建了我的实体,但是当我创建用户表时,它失败并且标题中指定了错误.我已经四处搜索,人们已经说过UserName,Name而且Email属性太长了.我已经厌倦了在这些列上设置最大长度,如下面的示例,但我无法让它工作.
IdentityModel:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("CaptureDBContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).IsUnicode(false);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).IsUnicode(false);
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我可以使用它来处理标准实体,但不能使用Identity表
这个问题不重复,因为链接中发布的教程是2年,MySQL.Data版本则不支持EF.我正在使用的那个.
我正在使用此代码进行登录.用户登录时如何找到用户角色?
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = await UserManager.FindByNameAsync(model.Username);
if (user != null)
{
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
ViewBag.errorMessage = "You must have a confirmed email to log on.";
return View("Error");
}
}
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc identity asp.net-identity asp.net-identity-2
虽然似乎有很多关于使用ASP.NET Core验证角色,声明等的文档,但是关于最初在我的应用程序中设置这些内容的信息很少.
我是Identity Server的新手,所以请耐心等待.尝试了解Identity Server 4中的以下两个库.
IdentityServer4.EntityFramework IdentityServer4.AspNetIdentity
据我所知,这有助于EF和AspNet.查看源代码和实体,它们都有完全不同的实体.AspNetIdentity有与User,Roles等相关的实体,而EntityFramework则包括Client,Resources等.但是两者都有UserClaim让我有点困惑.是否可以在项目中使用这两个库?或者只应该使用或其他?
如果我们可以使用两者,那么我们可以对两个库中的UserClaim实体做些什么呢?
谢谢,
c# entity-framework asp.net-identity identityserver3 identityserver4
我使用ASP.NET CORE Identity创建ASP.NET CORE应用程序.我创建了种子类,用于为第一个启动应用程序保存新用户和角色.在这个种子类中,当我向用户添加角色时,我会收到以下错误.
INSERT语句与FOREIGN KEY约束"FK_AspNetUserRoles_AspNetUsers_UserId"冲突.冲突发生在数据库"DB_A14695_Elvinm",表"dbo.AspNetUsers",列"Id"中.该语句已终止.
我使用以下类来识别身份
public class ApplicationUser : IdentityUser
{
public int Type { get; set; }
public int Flags { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime LastModifiedDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有我的种子班
public class DbSeeder
{
#region Private Members
private RvMusicalDbContext DbContext;
private RoleManager<IdentityRole> RoleManager;
private UserManager<ApplicationUser> UserManager;
#endregion Private Members
#region Constructor
public DbSeeder(RvMusicalDbContext dbContext, RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager)
{
DbContext = dbContext;
RoleManager = roleManager;
UserManager = …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc seeding asp.net-identity entity-framework-core
asp.net-identity ×10
c# ×6
asp.net-mvc ×5
asp.net ×4
.net ×1
asp.net-core ×1
automapper ×1
identity ×1
mysql ×1
seeding ×1