My.user.IsInRole 总是返回 false [英] My.user.IsInRole always returning false

查看:25
本文介绍了My.user.IsInRole 总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在处理一项迁移请求,我们需要将框架从 3.5 更改为 4.6.2.这里的问题是在更改以下方法的框架后没有按预期显示结果.My.User.IsInRole() 总是返回 false.

Currently, I'm working in one migration request, where we need to change the framework from 3.5 to 4.6.2. Here the problem is after changing the framework below method is not showing result as expected. My.User.IsInRole() is always returning false.

If My.User.IsInRole(nlRole.InnerText) Then
   hasRole = True
   Exit For
End If

此外,我使用以下代码进行了测试:

Also, I tested with below code:

Imports System.Security.Principal

Class PrincipalCheck
    Shared Function UserInRole(role As String) As Boolean
        Dim currPrincipal As New WindowsPrincipal(New WindowsIdentity(Environment.UserName))
        Return currPrincipal.IsInRole(role)
    End Function
End Class

Public Sub StartCheck()
   MsgBox(PrincipalCheck.UserInRole("MyDomain\MyGroup"))
End Sub

但没有运气.有人可以帮我解决上述问题.

But no luck. Can someone please help me with the above issue.

如果您希望我提供更多信息,请告诉我.

If you expect some more input from my end, please let me know.

推荐答案

怀疑问题可能与Environment.UserName的使用有关.尝试用 WindowsIdentity.GetCurrent() 替换它,因此:

I suspect that the problem may be related to the use of Environment.UserName. Try replacing that with WindowsIdentity.GetCurrent(), thus:

Shared Function UserInRole(role As String) As Boolean
    Dim currPrincipal As New WindowsPrincipal(WindowsIdentity.GetCurrent())
    Return currPrincipal.IsInRole(role)
End Function

但是,值得记住的是,用户帐户控制可能会成为障碍.如果您没有运行提升,则上述内容不适用于 WindowsBuiltInRole.Administrator,可能其他人.所以这也可能是一个问题.所以值得一看,当你跑高时是否得到不同的结果.

However, it is worth remembering that User Account Control can get in the way. If you aren't running elevated then the above won't work for the WindowsBuiltInRole.Administrator, possibly others. So that might be an issue as well. So worth seeing if you get different results when you run elevated.

这篇关于My.user.IsInRole 总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆