當前位置:編程學習大全網 - 源碼下載 - 如何用c#處理用戶的登陸權限問題,

如何用c#處理用戶的登陸權限問題,

可以用這兩個名稱空間的東西來做啊

using System.Security.Permissions;

using System.Security.Cryptography;

登陸成功後賦予權限

ClassLibrary.TestAppCredentials taCS = new ClassLibrary.TestAppCredentials("ss", new string[] { ClassLibrary.Roles.Admin.ToString() });

Thread.CurrentPrincipal = taCS;

PrincipalPermission pPerm = new PrincipalPermission(null, ClassLibrary.Roles.Admin.ToString(), true);

pPerm.Demand();

據個簡單的例子,檢查當前用戶是否具有Admin權限來決定顯示壹個按鈕:

this.btnShowAll.Visible = Thread.CurrentPrincipal.IsInRole(ClassLibrary.Roles.Admin.ToString());

當然了,這個Roles是妳自己定義的壹個枚舉類型而已。。。

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Text;

using System.Security;

using System.Security.Principal;

using System.Runtime.Serialization.Formatters.Binary;

using System.Xml.Serialization;

using System.IO;

namespace ClassLibrary

{

public enum Roles

{

Admin,

Manager,

Soles,

None

}

public class TestAppCredentials:IPrincipal

{

private GenericIdentity _Identity;

private StringCollection _roles = new StringCollection();

private DateTime _Created;

private bool _properlyInitted = false

public TestAppCredentials(string userName, string[] roles)

{

_Identity = new GenericIdentity(userName, "TestAppCredentials");

if (null != roles)

{

_roles.AddRange(roles);

}

_Created = DateTime.Now;

_properlyInitted = true;

}

#region IPrincipal 成員

[XmlIgnoreAttribute]

public IIdentity Identity

{

get

{

CheckProperlyInitted();

CheckExpired();

return _Identity;

}

}

public bool IsInRole(string role)

{

CheckProperlyInitted();

CheckExpired();

return _roles.Contains(role);

}

private void CheckProperlyInitted()

{

if (!_properlyInitted)

throw new SecurityException(

"Principal not properly initialized. Might be spoofed.");

}

private void CheckExpired()

{

if (_Created.AddHours(48) <= DateTime.Now)

throw new SecurityException("Credentials have expired.");

}

#endregion

}

}

  • 上一篇:WDF是什麽意思
  • 下一篇:擴展接口源代碼
  • copyright 2024編程學習大全網