大家好我需要帮助在c#中使用身份安全性使用全局声明,我可以使用以下代码从用户身上获取cllaims的值:
namespace Webapplicationsecurity2
{
public partial class VistaCandidato : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//var identity = (ClaimsIdentity)User.Identity;
//IEnumerable<Claim> claims = identity.Claims;
var claimRol = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.Role);
var claimCve = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.GivenName);
String Cveusuario = claimCve.Value.ToString();
*but the problem is that i need to make these vars as global in order to use them in other methods but if i put the above code outside of the pageload the system says i can not use type "var" as global, so i tried with the following way:*
namespace Webapplicationsecurity2
{
public partial class MasterPage : System.Web.UI.MasterPage
{
dbDataContext db;
// public const string claimRol;
public static Claim CveUsuario = ((ClaimsIdentity)HttpContext.Current.User).FindFirst(ClaimTypes.GivenName.ToString());
int idusuario = Convert.ToInt32(CveUsuario.Value);
public static Claim Rol = ((ClaimsIdentity)HttpContext.Current.User).FindFirst(ClaimTypes.Actor);
int rolusuario = Convert.ToInt32(Rol.Value);
protected void Page_Load(object sender, EventArgs e)
{
db = new dbDataContext();
将类型var更改为Claim并且似乎没问题但是当我运行应用程序时出现错误
无法转换类型&#39; System.Security.Claims.ClaimsPrincipal&#39;到&#39; System.Security.Claims.ClaimsIdentity&#39;。
请有人可以帮我解决这个问题吗?谢谢先进的
答案 0 :(得分:0)
我在页面加载方法中找到了一个Session var:
var claimRol = ((ClaimsIdentity)HttpContext.Current.User).FindFirst(ClaimTypes.Role);
Session["Rol"] = claimRol.Value.ToString();// this is the global var
但是有没有另一种方法来解决我的问题而不使用Session var?如果我使用var会话,则不需要使用声明,请有人帮助我。