asp.net-identity

Asp.net MVC 5 can't get user roles

二次信任 提交于 2019-12-04 18:18:40
I'm trying to get user roles and modify it. I've tried many ways to get user roles but nothing works. Is there anything missing? I can get right User entity but Roles is always null. Is there any way to do it correctly? Thanks var user = UserManager.Users.Single(u=>u.Id==id); var roles = user.Roles; roles.Add(....) var user = UserManager.Users.Single(u=>u.Id==id); user.IsinRole("rolename"); You can get them via claims: var roles = ((ClaimsIdentity)User.Identity).Claims .Where(c => c.Type == ClaimTypes.Role) .Select(c => c.Value); To add a user to a role, you can do ( Make sure the role exists

How to change error message in ASP.NET Identity

心不动则不痛 提交于 2019-12-04 18:07:01
问题 I have one question. I'm trying change error message in Identity ASP .NET and I don't know how do it. I want change error message - "Login is already taken". CreateAsync method return this error message. Please help me. 回答1: The Microsoft.AspNet.Identity.UserManager<TUser> class has a public property called UserValidator of type IIdentityValidator<TUser> . The constructor for UserManager sets that property to an instance of Microsoft.AspNet.Identity.UserValidator<TUser> . The error messages

How to access User in a service context in web API?

牧云@^-^@ 提交于 2019-12-04 17:20:39
问题 If you are in a controller context, you can access the current authenticated User. Take an article such as Get the current user, within an ApiController action, without passing the userID as a parameter . However, if my controller calls a service (same assembly), but here I don't have the controller context. What is the best way to actually get the authenticated user? 回答1: You can create an intermediate service to provide that functionality public interface IPrincipalProvider { IPrincipal

OpenIdConnectAuthenticationHandler: message.State is null or empty

不羁的心 提交于 2019-12-04 16:43:32
I am using UseOpenIdConnectAuthentication middleware for ASP.Net Core application to authenticate against Dells Cloud access manager token provider (setup to provide OpenId/OAuth2 authentication). Following is the code: app.UseCookieAuthentication(new CookieAuthenticationOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, AuthenticationScheme = "ClientCookie", CookieName = CookieAuthenticationDefaults.CookiePrefix + "ClientCookie", ExpireTimeSpan = TimeSpan.FromMinutes(5), LoginPath = new PathString("/signin"), LogoutPath = new PathString("/signout") }); app

Identity (on database creation) error specified key was too long

╄→гoц情女王★ 提交于 2019-12-04 15:41:20
I have here an issue with AspNet Identity and MySql, the issue is error which is really blocking me, this error is following: specified key was too long max key length is 767 bytes Now I followed this tutorial of using MySql with Identity: http://www.asp.net/mvc/tutorials/security/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider I want to say I had progression, because now atleast i get some tables generated in MySQL, but I'm stack on this issue, for a long time i'm trying to solve this, but without success. The error i get is in this part: public void

How to force an HTTPS callback using Microsoft.AspNetCore.Authentication.Google?

我怕爱的太早我们不能终老 提交于 2019-12-04 15:03:01
I am creating an AspNetCore application with Google authentication. I am deploying this app behind an nginx reverse proxy on an Ubuntu server. Almost everything is working, but I am having trouble with the callback url. In the Google developer console, I have http://localhost:5000/signin-google set as an authorized redirect URI. This works as expected and allows me to use Google authentication when running from my workstation. For production, I have https://myserver/signin-google set as an authorized redirect URI. However, when I try to use it, I get an error from accounts.google.com that http

Is ASP.NET Core Identity needed for Intranet app using Windows Authentication

孤人 提交于 2019-12-04 13:47:22
问题 Using Windows Authentication in an Intranet web application I want to achieve the following: Gather additional attributes from AD (name, employee number) Gather additional attributes from a database table (working hours, pay) Authorize based on application roles (not AD groups) Authorize based on an AD attribute (has direct reports) User not provide a username/password In my search for an answer it is suggested that I need to add ClaimsTransformation to my application: How do I use Windows

How to overload UserManager.AddToRoleAsync(string userId, string role)

*爱你&永不变心* 提交于 2019-12-04 13:18:09
I'm using Asp.net Identity Framework 2.1. I implement customized ApplicatoinUser, ApplicationRole, ApplicationUserRole, because I want to add support to multi-tenant, that is each user belongs to different companies, but I have 3 roles among all these companies, they are User, Admin and Approver. My ApplicationUserRole derived from IdentityUserRole, and have one more property: CompanyId. This property will indicate the user's role in this particular company. My code for these customized classes attached in bottom. My question is when I try to override ApplicationUserManager(Yes, it derived

Using a Web API Service as Central Authentication Point

可紊 提交于 2019-12-04 13:09:02
I'm very new to the identity management world, so please spare me. :) What I would like to do, is to have multiple client (MVC) applications that talk to a single Web API application to authenticate their users against. In that Web API application, I would like to use ASP.NET Identity to talk to a database with users. That way, I could use SSO for the client applications (I guess). Does that make sense? Could someone help me on my way to implement this (links are also welcome of course)? I don't know if IdentityServer could help me with what I want? And as a side question: when I could

Checking role exists for user before add

可紊 提交于 2019-12-04 13:03:49
I am trying to add roles for user but before that i want to check it is exists or not. How Can i do that? Here is my code public void AddRoleForUser(ApplicationUser obj, IdentityRole role) { _userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(_context)); var currentUser = _userManager.FindById(obj.Id); // before this i have to check var roleresult = _userManager.AddToRole(currentUser.Id, role.Name); } for example i have a user and its id =1. When i add role for this user i want to check there is a role for this user before add new role to this user Basanta Matia You just