Что такое GenericIdentity?

Вы можете достичь этого, используя псевдоэлементы ::after

body {
	margin: 0;
	padding: 0;
}
.centered {
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
	font-size: 45px;
	text-transform: uppercase;
	z-index: 10;
}
.container::after {
	content: '';
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.47);
}
.container {
    position: relative;
    text-align: center;
    color: white;
    height: 100%;
    display: block;
}
.center-fit {
    width: 100%;
    display: block;
}
Centered

24
задан Chad Grant 4 May 2009 в 07:38
поделиться

3 ответа

GenericIdentity и GenericPrincipal являются простейшим способом описания пользователя как «принципала». Это можно использовать для проверки безопасности в приложении, не зависящей от реализации, т. Е. Если пользователь входит в систему как «Fred» с разрешениями «Пользователь» и «Администратор»:

    string[] roles = { "User", "Admin" };
    Thread.CurrentPrincipal = new GenericPrincipal(
        new GenericIdentity("Fred"), roles);

Вы можете сделать это в момент входа клиента в систему. winform, или есть определенные пункты, чтобы сделать это в WCF, ASP.NET и т. д.

Затем более поздний код, без необходимости знать , как обрабатываются эти разрешения, может проверить это разрешение - либо через IsInRole , или декларативно:

[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
void SomeAdminFunction() { }

Здесь некоторые полезные служебные коды представляют собой безопасные для нулей обертки вокруг принципала / идентичности:

public static string GetUsername() {
    IPrincipal principal = Thread.CurrentPrincipal;
    IIdentity identity = principal == null ? null : principal.Identity;
    return identity == null ? null : identity.Name;
}
public static bool IsInRole(string role) {
    IPrincipal principal = Thread.CurrentPrincipal;
    return principal == null ? false : principal.IsInRole(role);
}

Тогда у вас может быть некоторый код аудита в вашем DAL: More sophisticated principal implementations might, for example, do "on demand" access checking - i.e. until you ask for the "Foo" role it doesn't know - it then finds out (by talking to a web-service, database, active-directory, etc) and caches the result for future access. This is useful when the list of potential roles is large, and the number of roles typically queried in reality is small.

You can also use a principal to store extra identity information that is only needed in certain contexts - for example, a security token. Callers might test the principal with as to see if it supports the extra data.

Using "principal" is useful because your logic processing code can talk about identity, without having to know whether this is winforms, ASP.NET, WCF, a windows service, etc - it is abstract. Additionally, some 3rd party code will also talk to the principal.

As another example - I wrote some example code here that shows how to use the principal to control access to winform controls via the designer (via an IExtenderProvider - which puts extra entries into the property grid in VS).

48
ответ дан 28 November 2019 в 23:10
поделиться

Вы можете использовать GenericIdentity в качестве конкретной реализации Identity, где вы хотите самостоятельно, программно, предоставить информацию о текущем пользователе. Неплохо, если вы сами идентифицировали и аутентифицировали пользователя по другим каналам.

3
ответ дан 28 November 2019 в 23:10
поделиться

Проверьте

http://msdn.microsoft.com/en-us/library/system.security.principal.genericidentity.aspx

. Там вы найдете несколько примеров. Он представляет общего пользователя.

Аутентификация и права доступа профиля.

0
ответ дан 28 November 2019 в 23:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: