这是我的设置:
public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationUserLogin : IdentityUserLogin<Guid>
{
}
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}
public class ApplicationRoleClaim : IdentityRoleClaim<Guid>
{
}
Run Code Online (Sandbox Code Playgroud)
这是我的UserStore的定义
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, MyContext, Guid>
{
public ApplicationUserStore(MyContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的UserManager的定义
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber …Run Code Online (Sandbox Code Playgroud) 我有一个使用Identity的ASP.NET Core应用程序.它可以工作,但是当我尝试将自定义角色添加到数据库时,我遇到了问题.
在Startup中,ConfigureServices我添加了Identity和角色管理器作为范围服务,如下所示:
services.AddIdentity<Entities.DB.User, IdentityRole<int>>()
.AddEntityFrameworkStores<MyDBContext, int>();
services.AddScoped<RoleManager<IdentityRole>>();
Run Code Online (Sandbox Code Playgroud)
在Startup中,Configure我注入RoleManager并将其传递给我的自定义类RolesData:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
RoleManager<IdentityRole> roleManager
)
{
app.UseIdentity();
RolesData.SeedRoles(roleManager).Wait();
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)
这是RolesData班级:
public static class RolesData
{
private static readonly string[] roles = new[] {
"role1",
"role2",
"role3"
};
public static async Task SeedRoles(RoleManager<IdentityRole> roleManager)
{
foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
{
var create = await roleManager.CreateAsync(new IdentityRole(role));
if (!create.Succeeded)
{
throw new Exception("Failed …Run Code Online (Sandbox Code Playgroud) 我无法理解两者之间的区别,asp.net identy基于OWIN,并且在IdentityServer介绍他时没有引入中间件,我很困惑..
我需要获得提出请求的用户ID.在以前的Identity版本中,我可以这样做:
User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)
但它似乎不再可用了.
还有类似的东西:
User.GetUserId();
Run Code Online (Sandbox Code Playgroud)
但它始终返回null,即使正确设置了User.Identity.IsAuthenticated并且正确设置了User.Identity.Name.
我应该用它做什么?
编辑:我的authenticaton逻辑基于[默认的Visual Studio 2015模板],到目前为止我的身份没有太大变化,所以如果你想检查它是如何完成的,你可以在我粘贴的github链接上看到它.
Visual Studio 2015脚手架使用UserManager<TUser>不能用于创建的脚手架ClaimsIdentity.有没有人有一个如何做到这一点的工作示例?
VS2015脚手架抛出错误:
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;
}
Run Code Online (Sandbox Code Playgroud)
注意:我添加了ApplicationUser与之不冲突的属性IdentyUser.
我在MVC 6(Asp.Net One Core)中有一个Web应用程序,我正在使用基于声明的身份验证.在Login方法中,我设置了声明:
var claims = new Claim[]
{
new Claim("Name", content.Name),
new Claim("Email", content.Email),
new Claim("RoleId", content.RoleId.ToString()),
};
var ci = new ClaimsIdentity(claims, "password");
await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(ci));
Run Code Online (Sandbox Code Playgroud)
现在,如果用户例如更改了用户配置文件中的电子邮件,我该如何更改"电子邮件"声明的电子邮件值?为了更新cookie,我必须再次使用SignOutAsync和SignInAsync吗?最好的解决方案是将其保存到经典会话中吗?还有更好的解决方案吗?我完全错了?
有什么建议?
c# asp.net asp.net-mvc claims-based-identity asp.net-identity-3
我已经浏览了identityServer4的文档,并且我将其设置为使用Microsoft Office 365作为登录提供程序.当用户登录后我想创建一个按钮,他可以让我的应用程序使用graph.microsoft.com的webhooks api订阅他的日历事件
startup.cs中的代码
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountOptions
{
AuthenticationScheme = "Microsoft",
DisplayName = "Microsoft",
SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
ClientId = "CLIENT ID",
ClientSecret = "CLIENT SECRET",
CallbackPath = new PathString("/signin-microsoft"),
Events = new OAuthEvents
{
OnCreatingTicket = context =>
{
redisCache.Set("AccessToken", context.AccessToken.GetBytes(), new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddDays(3)
});
return Task.FromResult(context);
}
}
Scope =
{
"Calendars.Read",
"Calendars.Read.Shared",
},
SaveTokens = true
});
Run Code Online (Sandbox Code Playgroud)
但这显然不是一条可行的道路.我只是为了测试目的而做了这个,并制作了所需订阅的PoC.
现在我想知道是否有更聪明的方式与identityServer通信,允许我获得这个外部访问令牌,以便我可以代表我的登录用户使用microsoft api?
或者,我唯一的选择是直接从此OAuthEvent获取Microsoft AccessToken并将其直接存储在链接到登录用户的数据库中?
我真的需要这个,因为我的大部分功能都是基于来自第三方的数据.
c# oauth asp.net-identity asp.net-identity-3 identityserver4
ASP.Net Core具有处理用户身份验证的SignInManager.其中一种方法是PasswordSignInAsync(string username, string password, bool isPersistent, bool lockoutOnFailure).将lockoutOnFailure设置为true应该在一定次数的登录尝试失败后暂时锁定用户.
查看数据库中的AspNetUsers表,我看到以下内容:
看起来预期的功能是允许5次登录尝试,然后将帐户锁定5分钟.
所以我的问题是:
我在路由AccessDenied时遇到了一些麻烦,可能还有Login/Logout路径.该项目是一个剥离的默认项目,没有更多的魔力.因此存在一个Account带AccessDenied()方法的控制器.
我现在正在尝试的是(这是互联网商品提供的解决方案)
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/");
options.AccessDeniedPath = new PathString("/InactiveSponsor");
options.LogoutPath = new PathString("/");
});
Run Code Online (Sandbox Code Playgroud)
但这绝对没有区别.那么任何想法?任何想法为什么不工作以及如何使其工作.
这是我的Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets …Run Code Online (Sandbox Code Playgroud)