logo
down
shadow

How to add Roles to Windows Authentication in ASP.NET Core


How to add Roles to Windows Authentication in ASP.NET Core

By : YYY
Date : November 20 2020, 11:01 PM
around this issue this is working code that I use to check is a user is in a role \ group, please use it at your leisure
code :
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Security.Principal;

namespace Santander.IsUserInGroupOrRole_cs
{

public class IsUserInRole
{
    public static bool IsInGroup(string groupName)
    {
        var myIdentity = GetUserIdWithDomain();
        var myPrincipal = new WindowsPrincipal(myIdentity);
        return myPrincipal.IsInRole(groupName);
    }

    public bool IsInGroup(List<string> groupNames)
    {
        var myIdentity = GetUserIdWithDomain();
        var myPrincipal = new WindowsPrincipal(myIdentity);

        return groupNames.Any(group => myPrincipal.IsInRole(group));
    }

    public static WindowsIdentity GetUserIdWithDomain()
    {
        var myIdentity = WindowsIdentity.GetCurrent();
        return myIdentity;
    }

    public static string GetUserId()
    {
        var id = GetUserIdWithDomain().Name.Split('\\');
        return id[1];
    }

    public static string GetUserDisplayName()
    {
        var id = GetUserIdWithDomain().Name.Split('\\');

        var dc = new PrincipalContext(ContextType.Domain, id[0]);
        var adUser = UserPrincipal.FindByIdentity(dc, id[1]);
        return adUser.DisplayName;

    }
}
}


Share : facebook icon twitter icon
Using Forms Authentication Cookie to Cache Roles - (Windows Authentication)

Using Forms Authentication Cookie to Cache Roles - (Windows Authentication)


By : Navjot Dhillon
Date : March 29 2020, 07:55 AM
ASP.NET Core Windows Authentication and Application Roles

ASP.NET Core Windows Authentication and Application Roles


By : Qscript
Date : March 29 2020, 07:55 AM
it fixes the issue You need to add a DefaultChallangeScheme to use Windows authentication. This is how i do, but if someone has a better solution i am all ears :)
I use the following setup in my current application.
code :
services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<SecurityDbContext>()
            .AddDefaultTokenProviders();

services.AddAuthentication(options =>
{
            options.DefaultChallengeScheme = IISDefaults.AuthenticationScheme;
});
services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
Windows authentication and roles authentication through stored procedure - mvc asp.net

Windows authentication and roles authentication through stored procedure - mvc asp.net


By : arnold rhee
Date : March 29 2020, 07:55 AM
I wish did fix the issue. If you have done things correctly in Visual Studio, a database schema will have been created for you on (most probably) your localDB. You will have to identify that database server and instance and you should see a corresponding table structure for users and roles. Once you have identified the database instance and confirmed the tables have been created and their names match you can create the following stored procedure (the table names may be different).
code :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE getRolesForUser
    @UserName nvarchar(50)
AS
BEGIN

    SET NOCOUNT ON;

        SELECT        AspNetRoles.Name
        FROM            AspNetRoles 
        INNER JOIN
                        AspNetUserRoles ON AspNetRoles.Id = AspNetUserRoles.RoleId 
        INNER JOIN
                        AspNetUsers ON AspNetUserRoles.UserId = AspNetUsers.Id
        WHERE AspNetUsers.UserName = @UserName 
END
GO
asp.net core 2, identity w/windows authentication but database managed roles

asp.net core 2, identity w/windows authentication but database managed roles


By : h224nh
Date : March 29 2020, 07:55 AM
To fix the issue you can do Configure the username validator to allow .
code :
services.AddIdentity<ApplicationUser, IdentityRole>(options => {
    options.User.AllowedUserNameCharacters = "allowed characters here";
});
ASP.NET Core 3 with Angular 8, ASP.NET Core Identity, Roles and token based authentication

ASP.NET Core 3 with Angular 8, ASP.NET Core Identity, Roles and token based authentication


By : user3257715
Date : March 29 2020, 07:55 AM
hope this fix your issue You could refer to below link which introduce to you a complete sample to use asp.net core Identity authentication ,JWT token authorization in asp.net core web api:
https://fullstackmark.com/post/13/jwt-authentication-with-aspnet-core-2-web-api-angular-5-net-core-identity-and-facebook-login
Related Posts Related Posts :
  • Entity Framework 7 scaffolding with original name
  • Drop event not firing on wpf grid
  • ToolTip as Timer
  • ASP.NET Find id of selected row from datalist
  • Do I understand Open-Closed Principle right?
  • How to read latest email from Yahoo mail using pop3 c#
  • XML deserialize - How to replace a value property by a class while keeping compatibility?
  • Refresh background colours on dropdown using Converter
  • In dotnet How many characters in a console column
  • Catch Alt + other key shortcut
  • Map an object to an object with a sub list
  • .NET Core get connection string from appsettings.json
  • Where can i find some themes/demo for UI in WPF application?
  • Console Application to retrieve Domain Shared Contacts (Google Apps Admin SDK)
  • How combine two different Paths
  • EntityFramework-Plus and ASP.NET Memory Cache expiration
  • How to pre-populate vimeo password protected video in the website
  • An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll
  • Getting the error message: “Could not find a part of the path"
  • Race conditition when using Process RedirectStandardError and RedirectStandardOutput,
  • How do I use an array from code in the xaml file? C#
  • How do I insert data when the primary key column is not an identity column?
  • recursively step through asp table (that has been populated with a query)
  • Redirect to login when unauthorized in ASP.NET Core
  • What is the difference between a RibbonWindow and Window?
  • Picking random objects from class
  • How to link to a header file in a C# using Visual Studio
  • Why GetEntryAssembly returns null?
  • Setting focus to textbox upon method-execution
  • How to get Column values stored in session datatable?
  • Asynchronous error handling in MVC 5 filters
  • Unable to animate the character in unity3d
  • C# Generic to Session
  • Change keys Extended WPF Toolkit Zoombox
  • At least one object must implement IComparable order by
  • What is the reference that need to be added for "using Microsoft.Web.WebPages.OAuth" to get facebook authentic
  • Windows 10 - Background Task write to App's Calendar
  • Custom helper works with input type but not with Html helper
  • How to get the guid of a process that is running
  • UWP - Template10 pageheader ellipsis tabstop on version 1.1.12
  • Check id from two tables, if there is no match delete it
  • Counting how many occurences have been between datetimes
  • Xamarin.form - Masterdetailpage : System.Reflection.TargetInvocationException: Exception has been thrown by the target o
  • Add asp.net LinkButton to a Literal using c#
  • Xamarin.Forms (WinPhone) Hide the Title space NavigationBar for Xamarin.Forms.NavigationPage. Please
  • Get connection string in .NET Core application
  • C# import dll function with const string& as parameter
  • How to navigate from browser to application in Citrix
  • Create buttons dynamically from code behind in ASP.NET
  • Filling datatable with int array.
  • Post data using WebClient in C# and server replay Bad Gateway
  • Collections Generic List anonymous type to System Data.IDataReader
  • How to call async method from not async method?
  • c# compare two lists based on sub items and return the difference
  • Entity Framework: Navigation Property from a service
  • Can't declare Session variable in asp.net sharepoint page
  • How to intercept filterchain for maintenance in a WebApi project
  • Entity Framework - Use Dynamic connection string with Oracle Provider
  • Inconsistent Convert.ToDouble(decimal) results
  • Get Child Directory from Path
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com