用户经过身份验证后的火灾事件。

时间:2022-06-23 09:14:19

Is there an event that fires after user is authenticated (Windows)? I am executing at Application_Start(), but it returns false User.Identity.IsAuthenticated.

在用户通过验证(Windows)之后是否会触发事件?我正在执行Application_Start(),但是它返回false User.Identity.IsAuthenticated。

What I would like is to create and add roles manually to a user.

我想要的是手动地为用户创建和添加角色。

2 个解决方案

#1


1  

I used this:

我用这个:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.User != null)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (HttpContext.Current.User.Identity is WindowsIdentity)
            {
                if (string.IsNullOrEmpty(Usuario.Nome))
                {

Make sure, to in you project properties (alt + enter in the project) click on WEB an check NTLM AUTHENTICATION.

确保在您的项目属性(alt + enter in the project)中单击WEB上的检查NTLM身份验证。

This worked for me. now HttpContext.Current.User.Identity is no longer null

这为我工作。现在HttpContext.Current.User。恒等式不再为空

#2


0  

Application_Start() is called when the first resource (such as a page) in an ASP.NET application is requested. Also, it’s called only once during the life cycle of an application. That has been said, you will not will be able to auth all the users at that point. The current Windows user information on the client computer is supplied by the web browser through a cryptographic exchange involving hashing with the Web server [Wiki]. It is only then that you can auth the user. So, User.Identity.IsAuthenticated should work on page load (see code below). You can extract the logic that you need to a common method and call it on the first time a page loads

Application_Start()在ASP中的第一个资源(比如页面)时被调用。网络应用程序请求。而且,在应用程序的生命周期中只调用一次。也就是说,在那个时候,您将无法获取所有的用户。客户端计算机上的当前Windows用户信息由web浏览器通过与web服务器进行哈希的加密交换提供。只有到那时,你才可以对用户进行操作。所以,User.Identity。IsAuthenticated应该用于页面加载(参见下面的代码)。您可以提取所需的逻辑,并在第一次加载页面时调用它。

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Page.Title = "Home page for " + User.Identity.Name;
        // add your logic here
    }
    else
    {
        // you get here if auth failed.
        Page.Title = "Home page for guest user.";
    }
}

More info after your update:

更新后的更多信息:

I would use Application_Start() to check and add roles if they do not already exist.

如果角色不存在,我将使用Application_Start()检查和添加角色。

if (! Roles.RoleExists("Auditors"))
            Roles.CreateRole("Auditors");

You might find this post useful: http://weblogs.asp.net/scottgu/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server

您可能会发现这篇文章很有用:http://weblogs.asp.net/scottgu/recipe_3a00_implementing-role_2d00_base - security -with- asp.net -2.0- usingwindow - authentication/sql - server。

#1


1  

I used this:

我用这个:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.User != null)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (HttpContext.Current.User.Identity is WindowsIdentity)
            {
                if (string.IsNullOrEmpty(Usuario.Nome))
                {

Make sure, to in you project properties (alt + enter in the project) click on WEB an check NTLM AUTHENTICATION.

确保在您的项目属性(alt + enter in the project)中单击WEB上的检查NTLM身份验证。

This worked for me. now HttpContext.Current.User.Identity is no longer null

这为我工作。现在HttpContext.Current.User。恒等式不再为空

#2


0  

Application_Start() is called when the first resource (such as a page) in an ASP.NET application is requested. Also, it’s called only once during the life cycle of an application. That has been said, you will not will be able to auth all the users at that point. The current Windows user information on the client computer is supplied by the web browser through a cryptographic exchange involving hashing with the Web server [Wiki]. It is only then that you can auth the user. So, User.Identity.IsAuthenticated should work on page load (see code below). You can extract the logic that you need to a common method and call it on the first time a page loads

Application_Start()在ASP中的第一个资源(比如页面)时被调用。网络应用程序请求。而且,在应用程序的生命周期中只调用一次。也就是说,在那个时候,您将无法获取所有的用户。客户端计算机上的当前Windows用户信息由web浏览器通过与web服务器进行哈希的加密交换提供。只有到那时,你才可以对用户进行操作。所以,User.Identity。IsAuthenticated应该用于页面加载(参见下面的代码)。您可以提取所需的逻辑,并在第一次加载页面时调用它。

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Page.Title = "Home page for " + User.Identity.Name;
        // add your logic here
    }
    else
    {
        // you get here if auth failed.
        Page.Title = "Home page for guest user.";
    }
}

More info after your update:

更新后的更多信息:

I would use Application_Start() to check and add roles if they do not already exist.

如果角色不存在,我将使用Application_Start()检查和添加角色。

if (! Roles.RoleExists("Auditors"))
            Roles.CreateRole("Auditors");

You might find this post useful: http://weblogs.asp.net/scottgu/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server

您可能会发现这篇文章很有用:http://weblogs.asp.net/scottgu/recipe_3a00_implementing-role_2d00_base - security -with- asp.net -2.0- usingwindow - authentication/sql - server。