-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Access Windows Auth User, when in C# Model? #15145
Comments
Hello @kevingates ... try injecting the https://docs.microsoft.com/aspnet/core/security/blazor/#procedural-logic Let's leave this open because you are correct in that the topic doesn't show such an example. Management/engineering will decide if we should have an example to go alongside the Razor component example shown in the Procedural logic section. |
@guardrex hi there, Thank you for keeping it open. For context, I'm developing a "Vendors" componentent page, and I am injecting into the component: When I create seed data, I'm creating new vendors on initialization of the component. On my Vendors component, I'm initializing a new vendor @inject ApplicationStateProvider AppStatePro;
.......
......
......
new Vendor() { .....fields....., Created = new UserActivity(AppStatePro)}; UserActivity modelusing Microsoft.AspNetCore.Components.Authorization;
namespace myApp
{
public class UserActivity
{
public string UserName { get; set; }
public DateTime DateTime { get; set; }
public UserActivity(AuthenticationStateProvider authenticationStateProvider)
{
DateTime = DateTime.Now;
UserName = authenticationStateProvider.GetAuthenticationStateAsync().Result.User.Identity.Name;
}
}
} This works for now, but I would like a more intuitive, cleaner solution. |
Did you try injecting |
Idk if that will work ... I haven't had a chance to play with the auth bits too much. I'm 🏃😅 on lots of other issues. However, it seems reasonable from what engineering provided. It's worth a test to see if it will work. |
Pardon my ignorance. I'm still relatively new to Dependency Injection. I'm currently scanning microsoft docs, but do you have a pseudo-code (or actual code) example of how I would do that directly into the class? Also, the few times I get the values of AuthorizationService (trying to implement this in different ways), I never actually see anything that would lead me down a User -> Identity -> Name type of path. Thank you again for all your time and assistance. |
Yeah ... I see now that the user is from Why pass it tho ... can u just assign it directly to the new namespace BlazorAuthTest
{
public class UserActivity
{
public string UserName { get; set; }
public DateTime DateTime { get; set; }
}
public class Vendor
{
public string Something { get; set; }
public UserActivity UserActivity { get; set; }
}
} @page "/"
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
<h1>Hello, world!</h1>
Welcome to your new app.
@code {
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
var x = new Vendor()
{
Something = "blah",
UserActivity = new UserActivity()
{
DateTime = DateTime.Now,
UserName = user.Identity.Name
}
};
}
} |
Thanks for helping out @kevingates , @guardrex. |
I'm using Asp.Net Core 3.0, Server Side Blazor. The doc does well showing how you can get the Identity name for use within an component. But I have some Models utilized within my components. Namely a "User Activity" model which has 2 properties: UserName and DateTime.
I cannot find in any Blazor documentation how I can access the username within this .cs file. Any guidance would be appreciated.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: