Posts

Showing posts with the label ASP.NET

ASP.NET .NET 6 OIDC Retrieve JWT AccessToken after SaveTokens

I was not able to find the docs for retrieving the JWT AccessToken in ASP.NET .NET 6 after they are saved so I decided to document it here. You may be familiar with the HttpContext.GetTokenAsync() method . This appears to have worked in previous version of .NET, but I could not get it to work in .NET 6. First, make sure you are saving the access tokens in the  AddOpenIdConnect configuration. builder     .Services     .AddAuthentication(options =>     {         options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;         options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;     })     .AddCookie()     .AddOpenIdConnect(options =>     {         options.SaveTokens = true;     }); Then, instead of HttpContext.GetTokenAsync(), call HttpContext.AuthenticateAsync(), and get the token out of the AuthenticateResult. var accessToken = authenticateResult?.Properties?.GetString(".Token.access_token"); Hope this helps, Aaron

ASP.NET MVC 5 User Admin

AKA (for google-fu): asp.net mvc 5 web site administration tool asp.net mvc 5 web configuration tool asp.net mvc 5 identity asp.net mvc 5 membership I have missed the asp.net Web Site Administration Tool that used to be included with Visual Studio. I created an alternative for asp.net mvc 4 you can find here . For asp.net mvc 5, I created an  Azure Site Extension  to provide the same functionality. It can be run from VS on your local machine as well as installed as a site extension on Azure. Links: extension:  https://www.siteextensions.net/packages/AspNetUserMaintenanceAzureSiteExtension/ github repo:  https://github.com/Stonefinch/AspNetUserMaintenanceAzureSiteExtension Hope this helps, Aaron UPDATE These are now published to nuget.org  https://www.nuget.org/packages/AspNetUserMaintenanceAzureSiteExtension/

ASP.NET MVC 4 Bulk Add New Users

Image
---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- I recently developed a website to replace the built in ASP.NET Configuration Website Administration Tool provided by Microsoft and Visual Studio. Source available here:  https://github.com/StoneFinch/SmpMaintenance Direct link to download source code: https://github.com/StoneFinch/SmpMaintenance/archive/master.zip This tool has the ability to add new users to a ASP.NET MVC 4 website in bulk.  For example, if you have a list of usernames and passwords that you want to set up new accounts for, you can use the "Bulk Add New Users" functionality. Users can also be added to Roles via this page.  Multiple Roles are separated by spaces. Hope this helps, Aaron

ASP.NET MVC 4 SimpleMembershipProvider Website Administration Tool, ASP.NET Configuration

---------------------- Updated for ASP.NET MVC 5 and Identity 2.0. Includes an Azure Site Extension: http://aaron-hoffman.blogspot.com/2016/08/aspnet-mvc-5-user-admin.html ---------------------- ---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- The built in ASP.NET Configuration Website Administration Tool provided by Microsoft and Visual Studio left something to be desired. I have developed an alternative that can be used with ASP.NET MVC 4 SimpleMembershipProvider. Code available here:  https://github.com/StoneFinch/SmpMaintenance Current functionality at the time of this writing:   - Add New Users   - Bulk Add New Users   - Search Users   - Add New Roles   - Edit Existing Users   - Reset User Password (on Edit User page)   - Add Users to Roles   - Remove Users from Roles Hope this helps, Aaron

ASP.NET MVC 4 SimpleMembershipProvider, Web Security Roles Add User To Role SQL, Remove User From Role SQL

Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's System.Web.Security.Roles.AddUserToRole() method and RemoveUserFromRole() method are called: Text below in bold is dynamic and represents the UserName and RoleName property passed into the AddUserToRole()  method (or the UserId,   RoleId associated with the UserName,   RoleName ). Roles.AddUserToRole("UserName", "RoleName"); exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(25)',@0=N' USERNAME ' exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(5)',@0=N' RoleName ' exec sp_executesql N'SELECT COUNT(*) FROM [UserProfile] u, webpages_UsersInRoles ur, webpages_Roles r Where (u.[UserName] = @0 and r.RoleName = @1 and ur.RoleId = r.RoleId and ur.UserId = u.[UserId])',N'@0 nvarchar(8),@1 nvarchar(8

ASP.NET MVC 4 SimpleMembershipProvider, Web Security Roles Create Role SQL, Delete Role SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's System.Web.Security.Roles.CreateRole() method and DeleteRole() method are called: Text below in bold is dynamic and represents the RoleName property passed into the CreateRole() method (or the RoleId associated with the RoleName ). Roles.CreateRole("RoleName"); exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(8)',@0=N' RoleName ' exec sp_executesql N'INSERT INTO webpages_Roles (RoleName) VALUES (@0)',N'@0 nvarchar(8)',@0=N' RoleName ' Roles.DeleteRole("RoleName"); exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(8)',@0=N

ASP.NET MVC 4 SimpleMembershipProvider, WebMatrix WebSecurity CreateUserAndAccount Create New User SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's WebMatrix.WebData.WebSecurity.CreateUserAndAccount() method is called: Text below in bold is dynamic and represents the UserName and Password properties passed to the CreateUserAndAccount() method as well as the UserId generated as part of user creation. Similarly, the name of the UserId and UserName columns, and the name of the UserProfile table may also be different based on your specific configuration. exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(23)',@0=N' UserName ' exec sp_executesql N'INSERT INTO [UserProfile] ([UserName]) VALUES (@0)',N'@0 nvarchar(23)',@0=N' UserName ' exec sp_executesq

ASP.NET MVC 4 SimpleMembershipProvider, WebMatrix WebSecurity Login SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's WebMatrix.WebData.WebSecurity.Login() method is called: The UserName below (in bold ) is dynamic, it represents the UserName parameter passed to the Login() method. Similarly, the name of the UserId and UserName columns, and the name of the UserProfile table may also be different based on your specific configuration. exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(25)',@0=N' UserName ' exec sp_executesql N'SELECT COUNT(*) FROM webpages_Membership WHERE (UserId = @0 AND IsConfirmed = 1)',N'@0 int',@0=1 SELECT m.[Password] FROM webpages_Membership m, [UserProfile] u WHERE m.UserId = 1 AND m.UserId =

ASP.NET MVC 4 Membership, Users, Passwords, Roles, Profile, Authentication and Authorization

---------------- The easy way: Azure Site Extension to Add/Edit Users and Roles:  http://aaron-hoffman.blogspot.com/2016/08/aspnet-mvc-5-user-admin.html https://www.nuget.org/packages/AspNetUserMaintenanceAzureSiteExtension/ (old:  https://www.siteextensions.net/packages/AspNetUserMaintenanceAzureSiteExtension/ ) ---------------- ---------------- TLDR: Search for this line in your project and update it: WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); ---------------- There is a new Membership Provider in ASP.NET MVC 4 that can be used for Membership, Users, Passwords, Roles, Profile, Authentication and Authorization!  It is called the Simple Membership Provider .  It uses the WebMatrix WebData WebSecurity class as a facade. The Old Way You will no longer need to execute the old .NET 2.0 aspnet_regsql.exe like this: C:\Windows\Microsoft.NET\Framewor