Store OpenID users into my DB like Stackoverflow does - asp.net

I have made a login system using OpenID using the following code:
<rp:OpenIdLogin
runat="server"
Identifier="https://www.google.com/accounts/o8/id"
Visible="true"
ExampleUrl=""
LabelText=" "
RegisterText="Register"
ExamplePrefix=" "
ID="OpenIdLogin1"
OnLoggedIn="OpenIdTextBox1_LoggedIn"
RequestEmail="Require"
RequestPostalCode="Request">
</rp:OpenIdLogin>
It takes the user to Google for authentication. I only want to store the user information like email, full name and sex in my DB.
I have written the following code to retrive email from Google but nothing is returned:
Imports System
Imports DotNetOpenAuth.OpenId.Extensions.AttributeExchange
Partial Class Food
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Session("FetchResponse") Is Nothing) Then
Return
End If
Dim fetchResponse As FetchResponse = CType(Session("FetchResponse"), FetchResponse)
Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email)
End Sub
Public Property Email() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
End Class

If you are using Open Id you can ask for certain information from the Service provider.So when it came back once user has authorize themself you can get the information being provided by OpenId service provider.
One of the good way to ask for additional information is to use Attribute exchange.
see here for details how you can make use of Attribute exchange for Google
Google OpenId and Attribute Exchange
Once you have those parameters being send back by Google all you need to extract them from the Request and save them in the DB for future use
OpenID protocol will share certain information of user so you have to make sure if this fit in to your system or not.

Related

Making use of service references in vb.net

I've been trying to make use of this API in order to create a small app that will be run at a set interval in order to monitor a value. I've been told to do it in vb.net using service references and have so far managed to make use of the method that returns the last publication time, but when I try to retrieve the actual information from the table I receive an error:
"Exception: Compression is Not Enabled,This Web Service expects
clients to support GZIP,Deflate Compression"
So far i've been finding it difficult to find any info that is applicable to my current setup so i'm not sure how to progress. Is there anything i'm missing or any resources that would help me get my head around this?
Here is the current code i'm using to get the values.
Private Sub Soap(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim svc As New FlowInfo.InstantaneousFlowWebServiceSoapClient()
Dim str As String = svc.GetLatestPublicationTime().ToString
MessageBox.Show(str)
Dim body As FlowInfo.EDPReportBE = svc.GetInstantaneousFlowData()
End Sub

Why is my session variable being shared by multiple users in asp.net application

I have a session variable with a datatable assigned to it. For some reason the results from the datatable (display to user in a GridView) are being shared accross multiple users who are logged in. I thought each session was independent? So when one user makes changes then the other users see those results added to their results. Not sure why. I am not using Application variables.
I initialize the Session variable in Global_asax, then populate it on a button command after the user has filled out the required entries.
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session("RDDT") = New DataTable
End Sub
End Class
As it stands, I did overlook something. There was a usercontrol that someone else had created that had Public variables. The application was treating them as static. All I did was remove them and changed where in use to Session varibles. The Application works as intended now.

Referencing other aspx controls events in a module

For an ASP.Net application using VB how can I reference an event or textbox not located within the same file.
For example when you are coding on say Default.aspx and you put a textbox on the page this works to reference it.
Dim username As String
username = Textbox1.Text
Ok but now I want to get the values and response and process it (amongst other tasks) in a separate module say security.vb.
how can I effectively call it from security.vb so it says username = "Default.aspx".Textbox1.Text
i have tried many versions to achieve this and Google'd but I don't know the correct terms to search so am not getting a good result.
The closest Stack question is Reference from Module but that doesn't have an answer. I know this must be so simple but it eludes me.
Another "module"? You're not using classes? Using classes would make this easy:
In Default.aspx.vb
username = Textbox1.Text
Dim security As New Security(username)
In Security.vb:
Public Sub New(ByVal username as String)
Me.username = username
End Sub
Private username as String
Then you can access the username variable in your Security class whenever you need it. (Note that since the username variable is not declared as Shared, it will only be valid for the current instance of the Security class that you created in Default.aspx.vb. You could make it Shared, but that would be a bad idea on a web server, since if you did, that would mean that only one user could be logged in at a time, and whenever Bob logs in, Alice's session suddenly starts displaying Bob's data!)

Public Shared Variable Shared Between Users?

I've taken over the maintenance of the website (ASP.NET VB) and on one particular page I noticed the below code
Inherits System.Web.UI.Page
Public Shared UserNumber As String
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
UserNumber = Session("UserNumber")
...
End Sub
My question is whether the variable UserNumber can be accessed or changed by any other user than the current one?
Many thanks
A Shared variable is the same as a static variable in C#.
These can be shared across all instances, therefore different users would share the same variable.
Looking at the variable name, I would assume you would need to remove this Shared
You are applying a Session variable value to the Shared String on Page_Init.
Therefore each time the user loads the page, their session variable will override the current value.
If you are not using this variable outside of this class, then I would recommend changing it to:
Protected UserNumber As String
EDIT My initial answer was incorrect. Trying again...
Technically, as #Curt indicated, a Shared variable is shared across instances of the class.
However, with the code as is, it is less likely that the value will be shared amongst users as it is set to each user's local copy of the value in their Session in Page_Init.
There is a possible "race condition" where after the shared variable UserNumber has been initialised in Page_Init, and another user submits a request which updates the value of that variable from their session, the first user will then see the second user's value. i.e. users can see other user's values for concurrent requests.
Instead, I recommend using a ReadOnly non-shared property to get the value from the Session once:
Private mUserNumber As String
Public ReadOnly Property UserNumber As String
Get
If String.IsNullOrEmpty(mUserNumber) Then
mUserNumber = Session("UserNumber")
End If
Return mUserNumber
End Get
End Property
This uses a pattern called "Lazy-loading", which I use in read-only properties a lot on pages to improve performance and readability of code.

Get UserID from ASP.Net Login control LoggedIn event

In ASP.Net, I am trying to get the UserId (i.e., the user GUID) of the user that just logged on, in the LoggedIn event of the Login control. That is, I want to grab the UserId before the user is moved to to the next page. This is the code I am using:
Protected Sub Login1_LoggedIn(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Login1.LoggedIn
Dim UserId As String
UserId = Membership.GetUser.ProviderUserKey.ToString()
End Sub
However, I get an "Object reference not set to an instance of an object" error. This same code works well when I use it in subsequent pages, when a logged in user is accessing these pages.
ScottGu has the answer to this problem, as explained in this blog post on the ASP.NET Forum:
The LoggedIn event fires immediately
after the user is logged in with the
login control -- but not before the
next request to the site. As such,
the User object isn't filled out yet.
If you change your code to something like:
Dim UserId As String
UserID = Membership.GetUser(Login1.UserName).ProviderUserKey.ToString()
It should work fine. Calling Membership.GetUser without passing the Username as a parameter will expect to grab the "current" logged in user. Of course, this fails for the above reasons as the User object is not yet populated. By passing the Login control's UserName property as a parameter to the GetUser() method, you explicitly force the Membership.GetUser method to retrieve the user as specified by name from the user store (i.e. database). This ensures that the Membership.GetUser method returns a valid MembershipUser object which allows you to access the ProviderUserKey property.

Resources