ASP.NET身份 - 无名氏型材 [英] ASP.NET Identity - Anonymous profiles

查看:147
本文介绍了ASP.NET身份 - 无名氏型材的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用匿名的配置文件来存储和使用配置文件数据无需身份验证。如何启用匿名型材在页面
中描述的 https://msdn.microsoft.com/en-us/library/ewfkf772(v = VS.100)的.aspx

I want to use anonymous profiles to store and use profile data without authenticated. How to enable anonymous profiles is described on page https://msdn.microsoft.com/en-us/library/ewfkf772(v=vs.100).aspx

配置文件也可以用匿名用户的工作。匿名
剖面的支持不是默认启用的,所以你必须明确地启用它。
此外,当你定义在Web.config
档配置文件属性,你必须明确地让他们单独为
匿名用户可用。配置文件属性不支持
默认的匿名访问,因为配置文件可能被设计为通过身份验证的
用户的工作,和许多特性都可能涉及到不可用匿名用户个人
信息。

Profiles can also work with anonymous users. Support for anonymous profiles is not enabled by default, so you must explicitly enable it. In addition, when you define profile properties in the Web.config file, you must explicitly make them available individually for anonymous users. Profile properties do not support anonymous access by default because profiles may be designed to work with authenticated users, and many properties are likely to pertain to personal information that is not available for anonymous users.

但我不知道,我可以使用配置文件数据的匿名用户。它说,在默认情况下,我不能使用配置文件数据。但默认情况下,必须使用配置文件数据为匿名用户,如果是,那么我该怎么办我的能力?
我迷茫......

But I'm not sure, that I can use profile data for anonymous users. It says, that by default I can't use profile data. But not by default, have I ability to use profile data for anonymous users and if "yes" then what should I do? I confused...

推荐答案

匿名配置文件是可能的。他们让我们用一个匿名用户配置文件相关联的数据(如邮政编码)。当然,个人资料数据才有意义,而ASP.NET可以识别通过cookie这样匿名用户(或查询字符串参数):

Answer

Anonymous profiles are possible. They let us associate profile data (e.g. a postal code) with an anonymous user. Of course, the profile data is only relevant while ASP.NET can identify the anonymous user via a cookie (or a query string parameter) like this:

但我不知道,我可以使用配置文件数据为匿名用户。

But I'm not sure, that I can use profile data for anonymous users.

我们能。我刚刚过我的本地机器与ASP.NET MVC上测试这一点。

We can. I've just tested this on my local machine with ASP.NET MVC.

...如果是,那么我该怎么办?

...and if "yes" then what should I do?

首先,添加以下到Web.config的的System.Web 部分文件。它告诉ASP.NET跟踪匿名用户,添加一个配置文件属性,并配置我们默认的数据库连接提供商(假设我们有一个名为一个连接字符串 DefaultConnection

First, add the following to the system.web section of the web.config file. It tells ASP.NET to track anonymous users, add a single profile property, and configure the provider with our default DB connection (assuming we have a connection string named DefaultConnection.)

<anonymousIdentification enabled="true" />   
<profile defaultProvider="SqlProvider" >
  <properties>
    <add name="PostalCode"
      type="System.String"
      allowAnonymous="true" />               
  </properties>
  <providers>
    <clear />
    <add name="SqlProvider"
         type="System.Web.Profile.SqlProfileProvider"
         connectionStringName="DefaultConnection" />
  </providers>      
</profile>  



一旦我们做到了这一点,ASP.NET将允许我们获取/设置文件属性如下

Once we've done that, ASP.NET will allow us to get/set profile properties as follows:

// get
var postalCode = Profile.GetPropertyValue("PostalCode") as string;

// set
Profile.SetPropertyValue("PostalCode", "V8K 1B1");
Profile.Save();



注意:您必须对数据库设置配置文件为这个工作(否则ASP.NET会抱怨说,它无法找到存储过程)设置配置文件的方法之一是 aspnet_regsql 上的数据库这样运行了。

Note: you must setup Profiles on your database for this to work (otherwise ASP.NET will complain that it cannot find stored procedures.) One way to setup Profiles is to run aspnet_regsql on your database like this.

aspnet_regsql -S "myDbServer" -A p -d "myDbName" -E

有关 aspnet_regsql 标记的详细信息,请运行 aspnet_regsql - ?相关文档。在 -A P 设置配置文件并在 -E 使用集成安全性。

For more info about the aspnet_regsql flags, run aspnet_regsql -? for documentation. The -A p sets up Profiles and the -E uses integrated security.

有关其他方法和更多的细节,这里有一些其他链接:

For other approaches and more details, here are some other links:

  • Walkthrough: Maintaining Web Site User Information with Profile Properties.
  • How to assign Profile values?
  • anonymousIdentification Element

这篇关于ASP.NET身份 - 无名氏型材的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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