Skip to content

Commit e313e94

Browse files
author
Justin Marks
authored
Merge pull request #19 from Microsoft/users/wismythe/oauth-web-sample-may29
OAuth web sample readme and web.config app property name tweaks
2 parents a9d6fb1 + 56bd4e9 commit e313e94

File tree

8 files changed

+53
-35
lines changed

8 files changed

+53
-35
lines changed

OAuthWebSample/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ $RECYCLE.BIN/
156156

157157
# Mac desktop service store files
158158
.DS_Store
159+
160+
161+
# =====
162+
# Other
163+
# =====
164+
storage*
165+
db.lock
166+
*.pubxml

OAuthWebSample/OAuthWebSample.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Configurations", "Configura
1616
PublishScripts\Configurations\OAuthSample-WAWS-dev.json = PublishScripts\Configurations\OAuthSample-WAWS-dev.json
1717
EndProjectSection
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9099F192-D034-4AB2-B628-EDB4B4C2BB81}"
20+
ProjectSection(SolutionItems) = preProject
21+
README.md = README.md
22+
EndProjectSection
23+
EndProject
1924
Global
2025
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2126
Debug|Any CPU = Debug|Any CPU

OAuthWebSample/OAuthWebSample/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class HomeController : Controller
1010
{
1111
public ActionResult Index()
1212
{
13-
ViewBag.AppId = System.Configuration.ConfigurationManager.AppSettings["AppId"];
13+
ViewBag.ClientAppId = System.Configuration.ConfigurationManager.AppSettings["ClientAppId"];
1414
ViewBag.CallbackUrl = System.Configuration.ConfigurationManager.AppSettings["CallbackUrl"];
1515
ViewBag.Scope = System.Configuration.ConfigurationManager.AppSettings["Scope"];
1616

OAuthWebSample/OAuthWebSample/Controllers/OAuthController.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public ActionResult Index()
1717
{
1818

1919
return View();
20-
2120
}
2221

2322
public ActionResult RequestToken(string code, string status)
@@ -70,7 +69,7 @@ private String PerformTokenRequest(String postData, out TokenModel token)
7069

7170
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(
7271
ConfigurationManager.AppSettings["TokenUrl"]
73-
);
72+
);
7473

7574
webRequest.Method = "POST";
7675
webRequest.ContentLength = postData.Length;
@@ -114,7 +113,7 @@ public String GenerateAuthorizeUrl()
114113
UriBuilder uriBuilder = new UriBuilder(ConfigurationManager.AppSettings["AuthUrl"]);
115114
var queryParams = HttpUtility.ParseQueryString(uriBuilder.Query ?? String.Empty);
116115

117-
queryParams["client_id"] = ConfigurationManager.AppSettings["AppId"];
116+
queryParams["client_id"] = ConfigurationManager.AppSettings["ClientAppId"];
118117
queryParams["response_type"] = "Assertion";
119118
queryParams["state"] = "state";
120119
queryParams["scope"] = ConfigurationManager.AppSettings["Scope"];
@@ -128,20 +127,19 @@ public String GenerateAuthorizeUrl()
128127
public string GenerateRequestPostData(string code)
129128
{
130129
return string.Format("client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion={0}&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={1}&redirect_uri={2}",
131-
HttpUtility.UrlEncode(ConfigurationManager.AppSettings["ClientSecret"]),
130+
HttpUtility.UrlEncode(ConfigurationManager.AppSettings["ClientAppSecret"]),
132131
HttpUtility.UrlEncode(code),
133132
ConfigurationManager.AppSettings["CallbackUrl"]
134-
);
133+
);
135134
}
136135

137136
public string GenerateRefreshPostData(string refreshToken)
138137
{
139138
return string.Format("client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion={0}&grant_type=refresh_token&assertion={1}&redirect_uri={2}",
140-
HttpUtility.UrlEncode(ConfigurationManager.AppSettings["ClientSecret"]),
139+
HttpUtility.UrlEncode(ConfigurationManager.AppSettings["ClientAppSecret"]),
141140
HttpUtility.UrlEncode(refreshToken),
142141
ConfigurationManager.AppSettings["CallbackUrl"]
143-
);
144-
142+
);
145143
}
146144
}
147145
}

OAuthWebSample/OAuthWebSample/OAuthWebSample.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<IISExpressWindowsAuthentication>disabled</IISExpressWindowsAuthentication>
2222
<IISExpressUseClassicPipelineMode>false</IISExpressUseClassicPipelineMode>
2323
<UseGlobalApplicationHostFile />
24+
<Use64BitIISExpress />
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2627
<DebugSymbols>true</DebugSymbols>
@@ -182,7 +183,7 @@
182183
<VisualStudio>
183184
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
184185
<WebProjectProperties>
185-
<UseIIS>True</UseIIS>
186+
<UseIIS>False</UseIIS>
186187
<AutoAssignPort>True</AutoAssignPort>
187188
<DevelopmentServerPort>43742</DevelopmentServerPort>
188189
<DevelopmentServerVPath>/</DevelopmentServerVPath>

OAuthWebSample/OAuthWebSample/Views/Home/Index.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ViewBag.Title = "Visual Studio Online OAuth Client Sample";
33

44
var missingMsg = "Not set - update web.config";
5-
var appIdVal = !String.IsNullOrEmpty(ViewBag.AppId) ? ViewBag.AppId : missingMsg;
5+
var clientAppIdVal = !String.IsNullOrEmpty(ViewBag.ClientAppId) ? ViewBag.ClientAppId : missingMsg;
66
var scopeVal = !String.IsNullOrEmpty(ViewBag.Scope) ? ViewBag.Scope : missingMsg;
77
var callbackUrlVal = !String.IsNullOrEmpty(ViewBag.CallbackUrl) ? ViewBag.CallbackUrl : missingMsg;
88
}
@@ -12,7 +12,7 @@
1212
<div class="jumbotron">
1313
<h1>Visual Studio Online OAuth Client Sample</h1>
1414
<p class="lead">This app shows how to authorize a user to authorize an app and then to request an access token to access Visual Studio Online on their behalf.</p>
15-
<p><a href="/oauth/requesttoken" class="btn btn-primary btn-large" >Start &raquo;</a></p>
15+
<p><a href="/oauth/requesttoken" class="btn btn-primary btn-large" >Authorize &raquo;</a></p>
1616
</div>
1717

1818
<div class="row">
@@ -24,7 +24,7 @@
2424
<li><a href="https://app.vsaex.visualstudio.com/app/register">Register</a> a client app with Visual Studio Online</li>
2525
<li>Update the web.config of this web app and set the App ID, Scope, App Secret, and Callback URL set in the registered app. The callback URL should be https://<i>site</i>/oauth/callback
2626
<ul>
27-
<li>App ID: <strong>@appIdVal</strong></li>
27+
<li>App ID: <strong>@clientAppIdVal</strong></li>
2828
<li>Scope: <strong>@scopeVal</strong></li>
2929
<li>Callback URL: <strong>@callbackUrlVal</strong></li>
3030
</ul>

OAuthWebSample/OAuthWebSample/Web.config

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
1212

1313
<!-- App Settings for OAuth-->
14-
<add key="AppId" value=""/>
15-
<add key="AppSecret" value=""/>
16-
<add key="ClientSecret" value=""/>
17-
<add key="Scope" value=""/>
14+
<add key="ClientAppId" value="INSERT HERE"/>
15+
<add key="ClientAppSecret" value="INSERT HERE"/>
16+
<add key="Scope" value="INSERT HERE"/>
17+
<add key="CallbackUrl" value="INSERT HERE"/>
1818
<add key="AuthUrl" value="https://app.vssps.visualstudio.com/oauth2/authorize"/>
19-
<add key="TokenUrl" value="https://app.vssps.visualstudio.com/oauth2/token"/>
20-
<add key="CallbackUrl" value=""/>
19+
<add key="TokenUrl" value="https://app.vssps.visualstudio.com/oauth2/token"/>
2120
</appSettings>
2221
<system.web>
2322
<compilation debug="true" targetFramework="4.5" />

OAuthWebSample/README.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
# ASP.NET web app (OAuth sample)
1+
# ASP.NET web app (VSTS OAuth sample)
22

33
This sample shows how to prompt a user to authorize a cloud service that can call APIs on Visual Studio Team Services on behalf of the user.
44

5-
To learn more about OAuth in Visual Studio Team Services, see [Authorize access with OAuth 2.0](https://www.visualstudio.com/docs/integrate/get-started/authentication/oauth)
5+
To learn more about OAuth in Visual Studio Team Services, see [Authorize access to VSTS with OAuth 2.0](https://docs.microsoft.com/vsts/integrate/get-started/authentication/oauth?view=vsts)
66

77
## How to setup
88

9-
> These instructions assume you will be deploying this sample app to an Azure web site. To learn more and to get started, visit [Get started with Azure Web Sites and ASP.NET](http://azure.microsoft.com/documentation/articles/web-sites-dotnet-get-started).
9+
> These instructions assume you will be deploying this sample app to an Azure web app. To learn more and to get started, visit [Get started with Azure Web Apps and ASP.NET](https://docs.microsoft.com/azure/app-service/app-service-web-get-started-dotnet-framework).
1010
1111
1. Register an OAuth client app in Visual Studio Team Services (https://app.vsaex.visualstudio.com/app/register)
12-
* The callback URL should be https://yoursite.azurewebsites.net/oauth/callback, where "yoursite" is the name of your Azure web site
13-
2. Clone this repository
14-
3. Open the solution (VstsOAuthSample.sln) in Visual Studio 2015 or later
15-
4. Update the following settings in web.config to match the values in the app you just registered:
16-
* App ID
17-
* App Secret (use the "Client Secret" shown on the VSTS Application Settings page, not the App Secret)
18-
* Scope (space separated)
19-
* Callback URL
20-
5. Build the solution (this will trigger a NuGet package restore, which will pull in all dependencies of the project)
21-
6. Deploy the app to Azure
12+
* The callback URL should be https://yoursite.azurewebsites.net/oauth/callback, where `yoursite` is the name of your Azure web app
13+
14+
2. Clone this repository and open the solution `OAuthWebSample\OAuthWebSample.sln` in Visual Studio 2015 or later
15+
16+
3. Update the following settings in web.config to match the values in the app you just registered:
17+
* `ClientAppID`
18+
* `ClientAppSecret` (use the "Client Secret" shown on the VSTS Application Settings page, not the App Secret)
19+
* `Scope` (space separated)
20+
* `CallbackUrl`
21+
22+
4. Build the solution (this will trigger a NuGet package restore, which will pull in all dependencies of the project)
23+
24+
5. Publish the app to Azure
2225

2326
### Run the sample
2427

25-
1. Navigate to the deployed app (https://yoursite.azurewebsites.net)
28+
1. Navigate to your app (https://yoursite.azurewebsites.net)
29+
2630
2. Confirm your App ID, scope, and callback URL are displayed properly
27-
![app](appstart.png)
28-
3. Click **Start**
31+
![app](appstart.png)
32+
33+
3. Click **Authorize**
34+
2935
4. Sign in to Visual Studio Team Services (if prompted)
36+
3037
5. Review and accept the authorization request
3138

3239
If everything is setup properly, Visual Studio Team Services will issue an access token and refresh token and both values will be displayed. **You should keep these values secret**. Also a new authorization will appear in [your profile page](https://app.vssps.visualstudio.com/Profile/View).

0 commit comments

Comments
 (0)