Code: Select all
public static bool IsUserInLocalGroup(string userName, string groupName)
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
{
using (GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName))
{
if (group != null)
{
return group.GetMembers(true).Any(m => m.SamAccountName.Equals(userName, StringComparison.OrdinalIgnoreCase));
}
}
}
return false;
}
< /code>
Es funktioniert im Debug -Modus einwandfrei, aber einmal in der Produktion IIS veröffentlicht, fällt es mit einem Fehler nicht aus < /p>
Die Der Netzwerkpfad wurde nicht gefunden < /p>
< /blockquote>
Ich denke, der Benutzer, der die App in IIS ausführt ApplicationPoolIdentity
Muss ich noch etwas tun? Jede Hilfe zur Lösung dieses Problems wird geschätzt.
Code: Select all
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy =>
policy.RequireAssertion(context =>
{
try
{
var userName = context.User.Identity?.Name?.Split('\\').Last();
return LocalGroupHelper.IsUserInLocalGroup(userName, "rWork-Admin");
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR]: {ex.Message} \n {ex.StackTrace}");
File.AppendAllText("logs/error_log.txt", $"[{DateTime.Now}] {ex.Message} \n {ex.StackTrace}\n");
return false;
}
}));
});