Skip to content
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

Closed
kevingates opened this issue Oct 16, 2019 — with docs.microsoft.com · 8 comments
Closed

How to Access Windows Auth User, when in C# Model? #15145

kevingates opened this issue Oct 16, 2019 — with docs.microsoft.com · 8 comments
Labels
Blazor doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue
Milestone

Comments

Copy link

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.

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Oct 16, 2019
@guardrex
Copy link
Collaborator

guardrex commented Oct 16, 2019

Hello @kevingates ... try injecting the IAuthorizationService into the class.

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.

@kevingates
Copy link
Author

@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: AuthenticationStateProvider.

When I create seed data, I'm creating new vendors on initialization of the component.
A property of a Vendor is a "Created" property, that is an instance of my UserActivity class.

On my Vendors component, I'm initializing a new vendor

@inject ApplicationStateProvider AppStatePro;
.......
......
......
new Vendor() { .....fields....., Created = new UserActivity(AppStatePro)};

UserActivity model

using 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.

@guardrex
Copy link
Collaborator

guardrex commented Oct 16, 2019

Did you try injecting IAuthorizationService directly into your UserActivity class and use it there (i.e., no need to pass it like that from the component)?

@guardrex
Copy link
Collaborator

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.

@kevingates
Copy link
Author

Did you try injecting IAuthorizationService directly into your UserActivity class and use it there (i.e., no need to pass it like that from the component)?

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.

@guardrex
Copy link
Collaborator

Yeah ... I see now that the user is from AuthenticationStateProvider in the example that Steve put up.

Why pass it tho ... can u just assign it directly to the new UserActivity .......

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
            }
        };
    }
}

@mkArtakMSFT
Copy link
Member

Thanks for helping out @kevingates , @guardrex.
This looks like a great question for the StackOverflow forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blazor doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
Archived in project
Development

No branches or pull requests

4 participants