我的目标是检查用户是否是特定活动目录组的成员。
在 .net mvc 中,我在服务内部使用了此代码
HttpContext.Current.Request.LogonUserIdentity.Groups
.Any(x => x.Translate(typeof(NTAccount)).Value == "some role"
,效果很好。
在 .net core mvc 2.1.2 中,我将IHttpContextAccessor
传递给服务构造函数并尝试使用以下内容
_httpAccessor.HttpContext.User.Identity.LogonUserIdentity.Groups
但是有一个问题,因为Identity
不包含LogonUserIdentity
。我试图找到任何解决方案,但未成功,如何获得用户组列表或检查用户是否为特定组的成员?
答案 0 :(得分:0)
除了使用通过“角色”检查权限的内置功能之外,如果要通过特定的广告组进行检查,还可以使用以下代码:
public class HomePage extends AppCompatActivity {
private static final int CHOOSE_IMAGE = 101;
private static final int LOAD_IMAGE = 102;
public TextView password;
public ImageView weatherView;
private ImageView newsView;
public NavigationView navigationView;
private ImageView profileImage;
private Uri uri;
private View headerLayout;
private FirebaseAuth mAuth;
private FirebaseUser user;
private FirebaseStorage storage;
private StorageReference mStorageRef;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
storage = FirebaseStorage.getInstance();
mStorageRef = storage.getReference();
//Hooking up the xml fields with java
weatherView = findViewById(R.id.weatherView);
newsView = findViewById(R.id.newsIcon);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
navigationView = (NavigationView)findViewById(R.id.nav_view);
headerLayout = navigationView.getHeaderView(0);
profileImage = (ImageView)headerLayout.findViewById(R.id.userProfilePic);
setSupportActionBar(toolbar);
setHeaderInfo(user);
profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setProfilePictureUpdate();
}
});
}
private void setProfilePictureUpdate() {
// this creates the intent to open all of the gallery pics
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Profile Picture"), CHOOSE_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null){
uri = data.getData();
// update the user profile pics
UserProfileChangeRequest profilePictureUpdate = new UserProfileChangeRequest.Builder().setPhotoUri(uri).build();
user.updateProfile(profilePictureUpdate).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
/*
The instruction below here is able to load the image just fine. it is not here where the image does not load, it is when i
log in again and i want to display the user information so i call the setHeaderInfo method. The name and email address
displays fine but the image saved to the user previously does not load and nothing appears in my imageview. its as if it
cant go back to the phones gallery and reload it from there.
*/
profileImage.setImageURI(user.getPhotoUrl());
Toast.makeText(HomePage.this, "Profile Picture Updated!", Toast.LENGTH_SHORT).show();
}
});
}
}
/**
*
* @param user current user
* method loads all of the user information such as name, email address and profile picture
*/
private void setHeaderInfo( FirebaseUser user) {
TextView email = headerLayout.findViewById(R.id.email_User);
TextView name = headerLayout.findViewById(R.id.name_User);
email.setText(user.getEmail());
System.out.println(user.getDisplayName());
name.setText(user.getDisplayName());
System.out.println("USER PHOTO URI"+ user.getPhotoUrl().toString());
// It is here where the image does not load and nothing appears for the profile image. I print the uri above and
// it does contain a image uri the problem is loading
//this is what prints out USER PHOTO URI content://com.android.providers.media.documents/document/image%3A208843 so i know it is saving it to user
Picasso.get().load(user.getPhotoUrl()).into(profileImage);
// tried to do the below but it requires Manage_Document permission which is only allow for special apps, not to sure what it meant.
// Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), user.getPhotoUrl());
//profileImage.setImageBitmap(bitmap);
}
并用作:
public static class Security
{
public static bool IsInGroup(this ClaimsPrincipal User, string GroupName)
{
var groups = new List<string>();
var wi = (WindowsIdentity)User.Identity;
if (wi.Groups != null)
{
foreach (var group in wi.Groups)
{
try
{
groups.Add(group.Translate(typeof(NTAccount)).ToString());
}
catch (Exception)
{
// ignored
}
}
return groups.Contains(GroupName);
}
return false;
}
}